我一直在尝试在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();
}