窗口正常打开,但在更改了一些变量名后,它很简单。我无法撤消更改,因为我已尝试重新启动PC。
我正在使用Microsoft Visual Studio 2013 Professional,msvc2012 plus SDL2和SDL_Image。 不知道是否重要,但笔记本电脑的配置是:i7,8gb ram,windows 8.1 pro,gtx 850M 4gb vram。
以下是代码:
Game.h
#ifndef GAME_H
#define GAME_H
#include "SDL.h"
class Game
{
public:
Game();
~Game();
bool init(const char* title, int xpos, int ypos, int width, int height, int flags);
void render();
void update();
void handleEvents();
void clean();
bool running() { return gameRunning; }
private:
SDL_Window * window;
SDL_Renderer * renderer;
SDL_Rect * player;
bool gameRunning;
};
#endif
Game.cpp
#include "Game.h"
Game::Game()
{
player->x = 0;
player->y = 0;
player->w = 32;
player->h = 32;
}
Game::~Game()
{
}
bool Game::init(const char* title, int xpos, int ypos, int width, int height, int flags)
{
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if (window != 0)
{
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer != 0)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
}
else { return false; }
} else { return false; }
} else { return false; }
gameRunning = true;
return true;
}
void Game::render()
{
SDL_RenderClear(renderer);
//SDL_RenderFillRect(renderer, &*player);
SDL_RenderPresent(renderer);
}
void Game::handleEvents()
{
SDL_Event event;
if (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
gameRunning = false;
break;
default:
break;
}
}
}
void Game::clean()
{
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
}
void Game::update()
{
}
的main.cpp
#include "Game.h"
Game* game;
int main(int argc, char* argv[])
{
game = new Game();
game->init("Lumia", 100, 100, 640, 480, 0);
while (game->running())
{
game->handleEvents();
game->update();
game->render();
}
game->clean();
return 0;
}
答案 0 :(得分:0)
在您的班级Game
中,player
是指针。
当你这样做时
Game::Game()
{
player->x = 0;
player->y = 0;
player->w = 32;
player->h = 32;
}
指针player
从未初始化,它没有指向有效对象,因此您无法取消引用它或未定义的行为(可能是您的调试器停止的原因)
在尝试分配player
之前,您需要先构建Game
,就像在使用它之前在main中构建一个新的{{1}}一样。