我正在努力用SDL在游戏窗口上渲染一个正方形。我有两个文件,SZ_Player.h和SZ.Player.cpp,然后是主文件。我还通过执行“ SZ_Player examplePlayer”来初始化SZ_Player,以及通过“包括” SZ_Player.h”来包括它。
我不确定为什么它没有显示出来,我在代码下面有图片:
SZ_Player.h
#ifndef playerFile
#define playerFile
#include <iostream>
#include "SDL.h"
class SZ_Player
{
public:
SZ_Player();
~SZ_Player();
void Init();
void Update();
void Input();
void Render(SDL_Renderer* pRenderer);
SDL_Rect player;
int x, y, height, width, R, G, B;
};
#endif
SZ_Player.cpp
#include "SZ_Player.h"
SZ_Player::SZ_Player()
{
}
SZ_Player::~SZ_Player()
{
}
void SZ_Player::Init()
{
player.x = 70;
player.y = 70;
player.w = 100;
player.h = 100;
R = 255;
G = 255;
B = 255;
}
void SZ_Player::Update()
{
}
void SZ_Player::Input()
{
}
void SZ_Player::Render(SDL_Renderer* pRenderer)
{
SDL_SetRenderDrawColor(pRenderer, R, G, B, 255);
SDL_RenderDrawRect(pRenderer, &player);
}
main.cpp
#include <iostream>
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
using namespace std;
#include "SZ_timer.h"
SZ_Timer aTimer;
const int DELTA_TIME = 10;
bool done = false;
#include "SZ_Player.h"
SZ_Player examplePlayer;
int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0);
// Creates the game window
SDL_Window* game_window = SDL_CreateWindow("Rise", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
// Creates the game render to draw on the game window.
SDL_Renderer* game_renderer = SDL_CreateRenderer(game_window, 0, 0);
// Game Loop
while (!done)
{
aTimer.resetTicksTimer();
SDL_SetRenderDrawColor(game_renderer, 0, 0, 20, SDL_ALPHA_OPAQUE);
SDL_RenderClear(game_renderer);
examplePlayer.Render(game_renderer);
SDL_RenderPresent(game_renderer);
// If less time has passed than allocated block, wait difference
if (aTimer.getTicks() < DELTA_TIME)
{
SDL_Delay(DELTA_TIME - aTimer.getTicks());
}
}
SDL_Quit();
// Exits program
return 0;
}