gpt4 book ai didi

c++ - 为什么游戏窗口在特定时间后关闭?

转载 作者:行者123 更新时间:2023-11-28 03:10:02 25 4
gpt4 key购买 nike

为什么 SDL Game Library 创建的窗口会在特定时间后自动关闭。
我知道原因是SDL_Delay()函数,但如果不使用那个函数,游戏会出现运行时错误。

如何创建一个在特定时间段不出现的持续工作的窗口?

我的代码(最简单的代码):

SDL_Window *window;
SDL_Renderer *render;

int main(int argc, char* args[]){

if(SDL_Init(SDL_INIT_EVERYTHING) >= 0){
window = SDL_CreateWindow("Simple game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
if(window != 0){
render = SDL_CreateRenderer(window, -1, 0);
}

}else{
return 1;
}

SDL_SetRenderDrawColor(render, 0, 0, 0, 255);
SDL_RenderClear(render);
SDL_RenderPresent(render);

SDL_Delay(3000);
SDL_Quit();

return 0
}

最佳答案

您需要永远循环并调用 SDL 更新屏幕函数。阅读此处的 LazyFoo 教程:http://lazyfoo.net/SDL_tutorials

或者这里有一段简短的代码可以帮助您入门:

#include <iostream>
#include "SDL/SDL.h" // basic SDL
#include <string>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BBP = 32; // bits per-pixel

SDL_Surface* screen = NULL; // display screen
SDL_Event event; // grab events


using namespace std;

bool init() {

// initialize SDL
if(SDL_Init( SDL_INIT_EVERYTHING ) == -1)
return false;

//the screen image
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT,
SCREEN_BBP, SDL_SWSURFACE );
if(!screen) {
cout << "error creating screen" << endl;
return false;
}
//Set the window caption
SDL_WM_SetCaption("Event Test", NULL );

return true;
}

int main(int argc, char* argv[])
{
try
{
// make sure the program waits for a quit
bool quit = false;

cout << "Starting SDL..." << endl;

// Start SDL
if(!init()) {
cout << "initialize error" << endl;
return false;
}

// main loop
while( quit == false )
{
if (SDL_PollEvent(&event))
{
// The x button click
if(event.type == SDL_QUIT)
{
//quit the program
quit = true;
}

}

// Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB(
screen->format, 0xFF, 0xFF, 0xFF ) );


//Update screen
if(SDL_Flip(screen) == -1)
return -1;
}
}
catch (exception& e)
{
cerr << "exception caught: " << e.what() << endl;
return -1;
}

return 0;
}

关于c++ - 为什么游戏窗口在特定时间后关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18730657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com