我已经编写了小型sdl应用程序。问题是启动时几乎所有东西都崩溃了。每个窗口都会丢失标题栏。应用程序有时会弹出,有时却不会。如果确实如此,则按q(退出键)将其关闭,但其他窗口仍保持“断开”状态。摆脱这种情况的唯一方法是注销,然后再次登录。 这是代码:
#include <bits/stdc++.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
const int screen_x = 800;
const int screen_y = 600;
SDL_Window* window = nullptr;
SDL_Texture* textures[3];
SDL_Texture* currentTexture = nullptr;
SDL_Renderer* renderer = nullptr;
SDL_Event event;
bool running = true;
SDL_Texture* loadTexture(const char* path)
{
SDL_Texture* texture = nullptr;
SDL_Surface* load = IMG_Load(path);
if (!load)
{
printf("Failed to load image %s. Error: %s\n", path, IMG_GetError());
}
else
{
texture = SDL_CreateTextureFromSurface(renderer, load);
if (!texture)
{
printf("Failed to convert surface from %s. Error: %s\n", path, SDL_GetError());
}
SDL_FreeSurface(load);
}
return texture;
}
bool loadMedia()
{
bool success = true;
currentTexture = textures[0] = loadTexture("./img/1.png");
if (!textures[0])
{
printf("Failed to load texture\n");
success = false;
}
textures[1] = loadTexture("./img/2.png");
if (!textures[1])
{
printf("Failed to load texture\n");
success = false;
}
textures[2] = loadTexture("./img/3.png");
if (!textures[2])
{
printf("Failed to load texture\n");
success = false;
}
return success;
}
bool init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
printf("Error: %s\n", SDL_GetError());
return false;
}
else
{
window = SDL_CreateWindow("SDL with linux", 0, 0, screen_x, screen_y, SDL_WINDOW_SHOWN);
if (!window)
{
printf("Error: %s\n", SDL_GetError());
return false;
}
else
{
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer)
{
printf("Error: %s\n", SDL_GetError());
return false;
}
else
{
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
int imgFlags = IMG_INIT_PNG;
if ((IMG_Init(imgFlags) & imgFlags) != imgFlags)
{
printf("Error: %s\n", SDL_GetError());
return false;
}
}
}
}
return true;
}
void close()
{
for (unsigned i = 0; i < 3; i++)
{
SDL_DestroyTexture(textures[i]);
textures[i] = nullptr;
}
SDL_DestroyRenderer(renderer);
renderer = nullptr;
SDL_DestroyWindow(window);
window = nullptr;
IMG_Quit();
SDL_Quit();
}
int main(int argc, char** argv)
{
if (!init())
{
printf("Initialization error\n");
}
else
{
if (!loadMedia())
{
printf("Media loading error\n");
}
else
{
while (running)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_q:
running = false;
break;
case SDLK_1:
currentTexture = textures[0];
break;
case SDLK_2:
currentTexture = textures[1];
break;
case SDLK_3:
currentTexture = textures[2];
break;
}
break;
}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, currentTexture, nullptr, nullptr);
SDL_RenderPresent(renderer);
SDL_Delay(80);
}
}
}
close();
return 0;
}
Makefile:
CC = g++
CFlags = -Wall -std=c++11
Input = main.cpp
Output = -o ./bin/prog
Linker = -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer
all:
$(CC) $(Input) $(CFlags) $(Linker) $(Output)
显示应用程序行为的屏幕截图
答案 0 :(得分:0)
对不起,这将更像是发表评论,但我没有在评论中发表评论的50声誉。您可以提供该窗口的屏幕截图吗? 如果不是,请尝试在x和y轴上使用SDL_WINDOWPOS_CENTERED,然后查看窗口是否正在显示。请分享一下是否可行