我一直在尝试在Visual Studio 2017中创建一个简单的C ++游戏,但我甚至无法获得简单的黑屏。窗户出现白色而且反应迟钝,有人能帮忙吗?我一直在学习Udemy的免费课程,它一直在努力。我的代码如下。
#include <iostream>
#include <SDL.h>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
const int screenWidth = 800;
const int screenLength = 600;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "SDL init faliure" << endl;
return 0;
}
SDL_Window *window = SDL_CreateWindow("Particle Fire", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screenWidth, screenLength, SDL_WINDOW_SHOWN);
SDL_Delay(100000);
if (window == NULL) {
SDL_Quit();
return 2;
}
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
SDL_Texture * texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC, screenWidth, screenLength);
if (renderer == NULL) {
cout << "Could not produce renderer";
SDL_DestroyWindow(window);
SDL_Quit();
return 3;
}
if (texture == NULL) {
cout << "Could not produce texture";
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 3;
}
Uint32 *buffer = new Uint32[screenWidth*screenLength];
memset(buffer, 0xFF, screenWidth*screenLength*sizeof(Uint32));
for (int i=0; i < screenWidth*screenLength; i++) {
buffer[i = 0xFFFF0000];
}
SDL_UpdateTexture(texture, NULL, buffer, screenWidth * sizeof(Uint32));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
bool quit = false;
SDL_UpdateTexture(texture, NULL, buffer, screenWidth * sizeof(Uint32));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture , NULL, NULL);
SDL_RenderPresent(renderer);
SDL_Event event;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
} }
}
delete buffer;
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
我尝试过更改所有内容,但它不起作用。
答案 0 :(得分:3)
创建窗口SDL_Delay(100000);
后,您有100秒的延迟。
此外,您需要将buffer[i = 0xFFFF0000]
更改为buffer[i] = 0xFFFF0000
。第一个只设置i
并保持缓冲区不变。第二个使像素变黄。