Anonymous
SDL3: SDL_INIT () возвращает NULL после миграции из SDL2 [Duplicate]
Сообщение
Anonymous » 10 июн 2025, 06:19
Я мигрирую свой проект с SDL2 в SDL3. После исправления других проблем у меня теперь есть проблема, где SDL_INIT () всегда возвращает NULL . Я проверил все активы, такие как (включая шрифт), присутствуют и правильно упоминаются, поэтому я считаю, что это не связанная с активами. После проверки всего актива я ожидал, что код запускается, но у него ошибка. Это мой код: < /p>
Код: Выделить всё
#include
#include
#include
#include
#include
#include
#include
#include
#include // C++20
constexpr int WIDTH = 960, HEIGHT = 720;
constexpr float APPLE_SPEED = 300.0f; // px/giây
constexpr float COLLIDE_DIST = 60.0f;
constexpr float RESET_Y = -100.0f;
int main(int, char**) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
SDL_Log("SDL_Init error: %s", SDL_GetError());
return 1;
}
if (TTF_Init() != 0) {
SDL_Quit();
return 1;
}
SDL_Window* win = SDL_CreateWindow("Cat Catcher", 960, 720, SDL_WINDOW_RESIZABLE);
if (!win) {
SDL_Log("SDL_CreateWindow error: %s", SDL_GetError());
TTF_Quit(); SDL_Quit();
return 1;
}
SDL_Renderer* ren = SDL_CreateRenderer(win, nullptr);
if (!ren) {
SDL_Log("SDL_CreateRenderer error: %s", SDL_GetError());
SDL_DestroyWindow(win); TTF_Quit(); SDL_Quit();
return 1;
}
SDL_SetRenderVSync(ren, true); // bật vsync rõ ràng
// Load textures + font
SDL_Texture* bg = IMG_LoadTexture(ren, "assets/spr_background.png");
SDL_Texture* apple = IMG_LoadTexture(ren, "assets/spr_apple.png");
SDL_Texture* cat = IMG_LoadTexture(ren, "assets/spr_cat.png");
TTF_Font* font = TTF_OpenFont("/usr/local/share/fonts/ARIAL.TTF", 24);
std::srand(static_cast(std::time(nullptr)));
float apple_x = 240 + std::rand() % (720 - 240), apple_y = RESET_Y;
float cat_x = 0, cat_y = 480;
float cat_speed = 0;
int score = 0;
float timeLeft = 60.0f;
bool can_collide = true, game_over = false;
SDL_Event e;
Uint64 last = SDL_GetTicks();
while (true) {
Uint64 now = SDL_GetTicks();
float dt = (now - last) / 1000.0f;
last = now;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_EVENT_QUIT) goto cleanup;
if (e.type == SDL_EVENT_KEY_DOWN) {
if (e.key.key == SDLK_LEFT) cat_speed = -400.0f;
if (e.key.key == SDLK_RIGHT) cat_speed = 400.0f;
}
if (e.type == SDL_EVENT_KEY_UP) {
if (e.key.key == SDLK_LEFT || e.key.key == SDLK_RIGHT)
cat_speed = 0.0f;
}
}
if (!game_over) {
cat_x += cat_speed * dt;
apple_y += APPLE_SPEED * dt;
timeLeft -= dt;
if (apple_y > HEIGHT) {
apple_y = RESET_Y;
apple_x = 240 + std::rand() % (720-240);
}
float dx = cat_x - apple_x;
float dy = cat_y - apple_y;
if (can_collide && std::hypot(dx, dy) < COLLIDE_DIST) {
score++;
apple_y = RESET_Y;
apple_x = 240 + std::rand() % (720-240);
can_collide = false;
}
if (apple_y > 200) can_collide = true;
if (timeLeft w, ss->h}, rt{0,20,ts->w, ts->h};
SDL_RenderTexture(ren, tsScore, nullptr, reinterpret_cast(&rs));
SDL_RenderTexture(ren, tsTime, nullptr, reinterpret_cast(&rt));
SDL_DestroyTexture(tsScore);
SDL_DestroyTexture(tsTime);
SDL_DestroySurface(ss);
SDL_DestroySurface(ts);
SDL_RenderPresent(ren);
}
cleanup:
SDL_DestroyTexture(bg);
SDL_DestroyTexture(apple);
SDL_DestroyTexture(cat);
TTF_CloseFont(font);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
TTF_Quit(); SDL_Quit();
return 0;
}
< /code>
output: < /p>
SDL_Init error:
Process finished with exit code 1
My environment:
OS: Ubuntu 25.04 (x86-64)
Compiler: Clang++ (20.1.7)
SDL3 version: 3.2.16
Any help ценится!
Подробнее здесь:
https://stackoverflow.com/questions/796 ... -from-sdl2
1749525567
Anonymous
Я мигрирую свой проект с SDL2 в SDL3. После исправления других проблем у меня теперь есть проблема, где SDL_INIT () всегда возвращает NULL . Я проверил все активы, такие как (включая шрифт), присутствуют и правильно упоминаются, поэтому я считаю, что это не связанная с активами. После проверки всего актива я ожидал, что код запускается, но у него ошибка. Это мой код: < /p> [code]#include #include #include #include #include #include #include #include #include // C++20 constexpr int WIDTH = 960, HEIGHT = 720; constexpr float APPLE_SPEED = 300.0f; // px/giây constexpr float COLLIDE_DIST = 60.0f; constexpr float RESET_Y = -100.0f; int main(int, char**) { if (SDL_Init(SDL_INIT_VIDEO) != 0) { SDL_Log("SDL_Init error: %s", SDL_GetError()); return 1; } if (TTF_Init() != 0) { SDL_Quit(); return 1; } SDL_Window* win = SDL_CreateWindow("Cat Catcher", 960, 720, SDL_WINDOW_RESIZABLE); if (!win) { SDL_Log("SDL_CreateWindow error: %s", SDL_GetError()); TTF_Quit(); SDL_Quit(); return 1; } SDL_Renderer* ren = SDL_CreateRenderer(win, nullptr); if (!ren) { SDL_Log("SDL_CreateRenderer error: %s", SDL_GetError()); SDL_DestroyWindow(win); TTF_Quit(); SDL_Quit(); return 1; } SDL_SetRenderVSync(ren, true); // bật vsync rõ ràng // Load textures + font SDL_Texture* bg = IMG_LoadTexture(ren, "assets/spr_background.png"); SDL_Texture* apple = IMG_LoadTexture(ren, "assets/spr_apple.png"); SDL_Texture* cat = IMG_LoadTexture(ren, "assets/spr_cat.png"); TTF_Font* font = TTF_OpenFont("/usr/local/share/fonts/ARIAL.TTF", 24); std::srand(static_cast(std::time(nullptr))); float apple_x = 240 + std::rand() % (720 - 240), apple_y = RESET_Y; float cat_x = 0, cat_y = 480; float cat_speed = 0; int score = 0; float timeLeft = 60.0f; bool can_collide = true, game_over = false; SDL_Event e; Uint64 last = SDL_GetTicks(); while (true) { Uint64 now = SDL_GetTicks(); float dt = (now - last) / 1000.0f; last = now; while (SDL_PollEvent(&e)) { if (e.type == SDL_EVENT_QUIT) goto cleanup; if (e.type == SDL_EVENT_KEY_DOWN) { if (e.key.key == SDLK_LEFT) cat_speed = -400.0f; if (e.key.key == SDLK_RIGHT) cat_speed = 400.0f; } if (e.type == SDL_EVENT_KEY_UP) { if (e.key.key == SDLK_LEFT || e.key.key == SDLK_RIGHT) cat_speed = 0.0f; } } if (!game_over) { cat_x += cat_speed * dt; apple_y += APPLE_SPEED * dt; timeLeft -= dt; if (apple_y > HEIGHT) { apple_y = RESET_Y; apple_x = 240 + std::rand() % (720-240); } float dx = cat_x - apple_x; float dy = cat_y - apple_y; if (can_collide && std::hypot(dx, dy) < COLLIDE_DIST) { score++; apple_y = RESET_Y; apple_x = 240 + std::rand() % (720-240); can_collide = false; } if (apple_y > 200) can_collide = true; if (timeLeft w, ss->h}, rt{0,20,ts->w, ts->h}; SDL_RenderTexture(ren, tsScore, nullptr, reinterpret_cast(&rs)); SDL_RenderTexture(ren, tsTime, nullptr, reinterpret_cast(&rt)); SDL_DestroyTexture(tsScore); SDL_DestroyTexture(tsTime); SDL_DestroySurface(ss); SDL_DestroySurface(ts); SDL_RenderPresent(ren); } cleanup: SDL_DestroyTexture(bg); SDL_DestroyTexture(apple); SDL_DestroyTexture(cat); TTF_CloseFont(font); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); TTF_Quit(); SDL_Quit(); return 0; } < /code> output: < /p> SDL_Init error: Process finished with exit code 1 [/code] My environment: [list] [*]OS: Ubuntu 25.04 (x86-64) [*]Compiler: Clang++ (20.1.7) [*]SDL3 version: 3.2.16 [/list] Any help ценится! Подробнее здесь: [url]https://stackoverflow.com/questions/79659733/sdl3-sdl-init-return-null-after-migration-from-sdl2[/url]