- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 sdl-2 中呈现文本.文本应出现在剪辑区域 SDL_Rect rcTextOutput
的 SDL_Rect destination
中,但它返回 sigabrt错误。我尝试用 valgrind 调试它和 gdb但我仍然没有看到问题。
我将把整个代码留在最后,但这是呈现文本的部分:
SDL_SetRenderDrawColor(gRenderer, 0x40, 0x40, 0x40, 0x40);
gTextSurface = TTF_RenderText_Solid(gFont, "text", gTextColor);
if (gTextSurface == NULL)
{
throw "Unable to render texture! ERROR: ";
}
gTextTexture = SDL_CreateTextureFromSurface(gRenderer, gTextSurface);
if (gTextTexture == NULL)
{
throw "Unable to render texture! ERROR: ";
}
SDL_Rect destination;
destination.x = rcTextOutput.x + rcTextOutput.w - 800;
destination.y = rcTextOutput.y;
destination.w = 800;
destination.h = 20;
SDL_RenderSetClipRect(gRenderer, &rcTextOutput);
SDL_RenderCopy(gRenderer, gTextTexture, NULL, &destination);
SDL_RenderSetClipRect(gRenderer, NULL);
// ...
SDL_RenderPresent(gRenderer);
我注意到了来自 this answer 的 SDL_SetClipRect()
及其示例,但它不起作用。
我已经看过这个 Lazy Foo' tutorial并且代码似乎是有序的。我也跑过 gdb在它上面并返回:
Starting program: /home/pradana/Projects/sierra/caventure/build/caventure
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
[New Thread 0x7fffeb5f1700 (LWP 25319)]
[New Thread 0x7fffeadf0700 (LWP 25320)]
terminate called after throwing an instance of 'char const*'
Thread 1 "caventure" received signal SIGABRT, Aborted.
0x00007ffff6c92a10 in raise () from /usr/lib/libc.so.6
我什至试过valgrind :
==25239== Memcheck, a memory error detector
==25239== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==25239== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==25239== Command: build/./caventure
==25239==
==25239== Syscall param writev(vector[...]) points to uninitialised byte(s)
==25239== at 0x5CF218D: ??? (in /usr/lib/libc-2.25.so)
==25239== by 0x8088BAC: ??? (in /usr/lib/libxcb.so.1.1.0)
==25239== by 0x8088FAC: ??? (in /usr/lib/libxcb.so.1.1.0)
==25239== by 0x808902C: xcb_writev (in /usr/lib/libxcb.so.1.1.0)
==25239== by 0x7D7EF3D: _XSend (in /usr/lib/libX11.so.6.3.0)
==25239== by 0x7D7F431: _XReply (in /usr/lib/libX11.so.6.3.0)
==25239== by 0x7D6A2EE: XInternAtom (in /usr/lib/libX11.so.6.3.0)
==25239== by 0x4EFB79A: ??? (in /usr/lib/libSDL2-2.0.so.0.4.1)
==25239== by 0x4EFC694: ??? (in /usr/lib/libSDL2-2.0.so.0.4.1)
==25239== by 0x4EEB87F: ??? (in /usr/lib/libSDL2-2.0.so.0.4.1)
==25239== by 0x4EEB60E: ??? (in /usr/lib/libSDL2-2.0.so.0.4.1)
==25239== by 0x4E4F1C6: ??? (in /usr/lib/libSDL2-2.0.so.0.4.1)
==25239== Address 0x79c5573 is 35 bytes inside a block of size 16,384 alloc'd
==25239== at 0x4C2CF35: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==25239== by 0x7D6F385: XOpenDisplay (in /usr/lib/libX11.so.6.3.0)
==25239== by 0x4EFA84F: ??? (in /usr/lib/libSDL2-2.0.so.0.4.1)
==25239== by 0x4EEB5BB: ??? (in /usr/lib/libSDL2-2.0.so.0.4.1)
==25239== by 0x4E4F1C6: ??? (in /usr/lib/libSDL2-2.0.so.0.4.1)
==25239== by 0x4011F3: init() (in /home/pradana/Projects/sierra/caventure/build/caventure)
==25239== by 0x40148A: main (in /home/pradana/Projects/sierra/caventure/build/caventure)
==25239==
terminate called after throwing an instance of 'char const*'
==25239==
==25239== Process terminating with default action of signal 6 (SIGABRT): dumping core
==25239== at 0x5C41A10: raise (in /usr/lib/libc-2.25.so)
==25239== by 0x5C43139: abort (in /usr/lib/libc-2.25.so)
==25239== by 0x53ED4EC: __gnu_cxx::__verbose_terminate_handler() (vterminate.cc:95)
==25239== by 0x53EB2A5: __cxxabiv1::__terminate(void (*)()) (eh_terminate.cc:47)
==25239== by 0x53EB2F0: std::terminate() (eh_terminate.cc:57)
==25239== by 0x53EB507: __cxa_throw (eh_throw.cc:87)
==25239== by 0x4013FC: loadMedia() (in /home/pradana/Projects/sierra/caventure/build/caventure)
==25239== by 0x40148F: main (in /home/pradana/Projects/sierra/caventure/build/caventure)
==25239==
==25239== HEAP SUMMARY:
==25239== in use at exit: 13,232,031 bytes in 33,989 blocks
==25239== total heap usage: 76,191 allocs, 42,202 frees, 50,434,525 bytes allocated
==25239==
==25239== LEAK SUMMARY:
==25239== definitely lost: 16 bytes in 1 blocks
==25239== indirectly lost: 176 bytes in 4 blocks
==25239== possibly lost: 4,143,500 bytes in 29,652 blocks
==25239== still reachable: 9,088,339 bytes in 4,332 blocks
==25239== suppressed: 0 bytes in 0 blocks
==25239== Rerun with --leak-check=full to see details of leaked memory
==25239==
==25239== For counts of detected and suppressed errors, rerun with: -v
==25239== Use --track-origins=yes to see where uninitialised values come from
==25239== ERROR SUMMARY: 5 errors from 1 contexts (suppressed: 0 from 0)
这是脚本的其余部分(删除了不相关的代码):
#include <stdio.h>
#include <string>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
// Screen dimensions, constants
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 900; // 600 for ground, 280 for output, 20 for input
SDL_Surface* gScreenSurface = NULL; // The surface contained by the window
SDL_Surface* gCurrentSurface = NULL; // Current displayed image
SDL_Surface* gTextSurface = NULL; // SDL_Surface for the text.
SDL_Texture* gTextTexture = NULL; // SDL_Texture for the text.
TTF_Font* gFont = NULL; // Font pointer.
SDL_Color gTextColor = { 0, 0, 0, 0xFF }; // Text color, white.
SDL_Renderer* gRenderer = NULL; // The renderer we'll be using
SDL_Rect rcGround, rcSprite, rcTextInput, rcTextOutput;
void init();
void loadMedia();
void quit();
void init()
{
if (SDL_Init(SDL_INIT_VIDEO) > 0)
{
throw "SDL failed to initialise! ERROR: ";
}
else
{
// CODE REDACTED: gWindow and gRenderer is initialised
if (TTF_Init() > 0)
{
throw "TTF could not be initialised! ERROR: ";
}
}
}
void loadMedia()
{
// Ground rendering
rcGround.x = 0;
rcGround.y = 0;
rcGround.w = 800;
rcGround.h = 600;
// Sprite rendering
rcSprite.x = 400;
rcSprite.y = 300;
rcSprite.w = 4;
rcSprite.h = 4;
// TextOutput box rendering
rcTextOutput.x = 0;
rcTextOutput.y = 600;
rcTextOutput.w = 800;
rcTextOutput.h = 280;
// TextInput box rendering
rcTextInput.x = 0;
rcTextInput.y = 880;
rcTextInput.w = 800;
rcTextInput.h = 20;
gFont = TTF_OpenFont("resources/consolas.ttf", 14);
if (gFont == NULL)
{
throw "Font load error";
}
}
void quit()
{
// Destroy window
SDL_DestroyWindow(gWindow);
SDL_DestroyRenderer(gRenderer);
SDL_DestroyTexture(gTextTexture);
SDL_FreeSurface(gTextSurface);
TTF_CloseFont(gFont);
gWindow = NULL;
gRenderer = NULL;
gFont = NULL;
// Quit SDL subsystems
TTF_Quit();
SDL_Quit();
}
int main()
{
try
{
init();
loadMedia();
bool quit = false;
SDL_Event event;
while(!quit)
{
// CODE REDACTED: event & sprite movement
}
if (rcSprite.x < 0 || rcSprite.y < 0 || rcSprite.y > rcGround.h || rcSprite.x > rcGround.w)
{
rcSprite.x = 400;
rcSprite.y = 300;
}
SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(gRenderer);
SDL_RenderFillRect(gRenderer, &rcGround);
SDL_BlitSurface(gCurrentSurface, NULL, gScreenSurface, &rcGround);
SDL_RenderFillRect(gRenderer, &rcTextInput);
SDL_BlitSurface(gCurrentSurface, NULL, gScreenSurface, &rcTextInput);
SDL_SetRenderDrawColor(gRenderer, 0x40, 0x40, 0x40, 0x40);
gTextSurface = TTF_RenderText_Solid(gFont, "text", gTextColor);
if (gTextSurface == NULL)
{
throw "Unable to render texture! ERROR: ";
}
gTextTexture = SDL_CreateTextureFromSurface(gRenderer, gTextSurface);
if (gTextTexture == NULL)
{
throw "Unable to render texture! ERROR: ";
}
SDL_Rect destination;
destination.x = rcTextOutput.x + rcTextOutput.w - 800;
destination.y = rcTextOutput.y;
destination.w = 800;
destination.h = 20;
SDL_RenderSetClipRect(gRenderer, &rcTextOutput);
SDL_RenderCopy(gRenderer, gTextTexture, NULL, &destination);
SDL_RenderSetClipRect(gRenderer, NULL);
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderDrawLine(gRenderer, 0, 600, 800, 600);
SDL_RenderDrawLine(gRenderer, 0, 880, 800, 880);
SDL_RenderFillRect(gRenderer, &rcSprite);
SDL_BlitSurface(gCurrentSurface, NULL, gScreenSurface, &rcSprite);
SDL_RenderPresent(gRenderer);
}
}
catch (const std::string& msg)
{
printf("%s", msg.c_str());
if (SDL_GetError() != NULL)
{
printf("%s", SDL_GetError());
}
else if (TTF_GetError() != NULL)
{
printf("%s", TTF_GetError());
}
else
{
printf("%s", "NULL");
}
quit();
exit(EXIT_FAILURE);
}
quit();
return 0;
}
最佳答案
您抛出一个指向字符数组的指针,但 try catch 对 ::std::string
的引用。这显然是行不通的,并且您的程序由于未捕获的异常而终止。您应该在抛出时显式构造一个字符串,或者实际上您应该抛出并捕获一个可以包含描述的 std::runtime_error
实例。
throw(::std::runtime_error("Unable to render texture!"));
...
catch(::std::runtime_error const & exception)
{
printf("%s", exception.what());
关于c++ - 抛出异常返回内存异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43830844/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!