- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
终端输出:
> ./a.out
bla1
Speicherzugriffsfehler
Speicherzugriffsfehler 在德语中的意思基本上是段错误。
当我使用全局变量,或将函数中的代码复制粘贴到 main 中时,它就可以正常工作。我不知道它为什么坏了。
在 Linux Mint 16 上编译:g++ sdl.cpp -lSDL2
源代码:
#include <iostream>
#include <SDL2/SDL.h>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
using namespace std;
bool init_SDL(SDL_Window *window, SDL_Surface *windowSurface);
void close_SDL(SDL_Window *window, SDL_Surface *image);
int main(int argc, char *argv[])
{
SDL_Window *window = NULL;
SDL_Surface *windowSurface = NULL;
SDL_Surface *image = NULL;
if(!init_SDL(window, windowSurface)){
cout << "Failed to initialize SDL" << endl;
}
else{
cout << "bla1" << endl;
SDL_FillRect(windowSurface, NULL, SDL_MapRGB(windowSurface->format, 0xFF, 0xFF, 0xFF));
cout << "bla2" << endl;
SDL_UpdateWindowSurface(window);
cout << "bla3" << endl;
SDL_Delay(2000);
}
close_SDL(window, image);
EXIT_SUCCESS;
}
bool init_SDL(SDL_Window *window, SDL_Surface *windowSurface){
bool success = true;
if(SDL_Init(SDL_INIT_VIDEO) < 0){
cout << "SDL failed to initialize! SDL_Error: " << SDL_GetError() << endl;
success = false;
}
else{
window = SDL_CreateWindow("SDL Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(window == NULL){
cout << "Failed to create window! SDL_Error: " << SDL_GetError() << endl;
success = false;
}
else{
windowSurface = SDL_GetWindowSurface(window);
}
}
return success;
}
void close_SDL(SDL_Window *window, SDL_Surface *image){
SDL_FreeSurface(image);
SDL_DestroyWindow(window);
SDL_Quit();
return;
}
最佳答案
我在 reddit 上找到了解决方案。这解决了它:
bool init_SDL(SDL_Window *&window, SDL_Surface *&windowSurface)
我需要通过引用传递指针,因为指针本身在函数中被操纵。否则他们仍然会指向 main 中的 null。
关于c++ - SDL_FillRect 中的 SDL2 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23070130/
我试图通过使用 SDL_FillRect() 和以下代码来覆盖 SDL_Surface * 中的小矩形: int display(SDL_Surface * screen, Uint1
我在使用链接列表在 SDL 中将“正文”(单个正方形)输出到屏幕时遇到问题。 我有一个 node,它有两个数据变量:一个指向下一个的 node 指针,和一个 SDL_Rect 值。 这就是我调用 SD
我正在使用 C++ 使用 SDL 制作游戏,我想使用 SDL_FillRect() 渲染粒子。我已经玩了几个小时的代码,但无论我做什么,粒子都没有绘制。这是我的 Render 函数中的代码(我确定我实
我正在使用 C 语言的 SDL2 库。 我制作了一个测试程序来打开一个白色窗口,但是我在函数 SDL_FillRect 中遇到了段错误,即使我在构建它时没有任何错误或警告。 代码如下: #includ
我的代码并没有真正起作用(没有错误,但没有显示矩形)。我有 3 个类:CFramework、CRectangle、CGame。有什么问题/缺失? 我的 CFramework 类启动 SDL 并设置视频
终端输出: > ./a.out bla1 Speicherzugriffsfehler Speicherzugriffsfehler 在德语中的意思基本上是段错误。 当我使用全
这个问题在这里已经有了答案: SDL2 program only works if Renderer is created with SDL_RENDERER_SOFTWARE (2 个答案) 关闭
我是一名优秀的程序员,十分优秀!