Hello,
I am new to C++ and have recently been following stayscrisp's Beginning SDL tutorials and tried a couple of others. I have spent hours trying to figure out why the images that I am trying to display always come up as white spaces.
I have tried placing the bitmap file to where my main is, also adding it as a resource to visual studio - without success. Here is my code:
I am new to C++ and have recently been following stayscrisp's Beginning SDL tutorials and tried a couple of others. I have spent hours trying to figure out why the images that I am trying to display always come up as white spaces.
I have tried placing the bitmap file to where my main is, also adding it as a resource to visual studio - without success. Here is my code:
//Game.h
#ifndef _GAME_H_
#define _GAME_H_
#include <SDL.h>
#include "SDL_Graphics.h"
class Game
{
public:
Game();
bool Running() {return m_bRunning;}
void Quit() { m_bRunning = false;}
void Init(const char* title, int width, int height, int bpp, bool fullscreen);
void HandleEvents(Game* game);
void Update();
void Draw();
void Clean();
private:
bool m_bRunning;
SDL_Surface* m_pScreen;
bool m_bFullscreen;
SDL_Surface* testSprite;
};
#endif
//Game.cpp
#include "Game.h"
// constructor
Game::Game()
{
}
void Game::Init(const char* title, int width, int height,
int bpp, bool fullscreen)
{
int flags = 0;
//initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
SDL_WM_SetCaption(title, title);
if(fullscreen) {
flags = SDL_FULLSCREEN;
}
m_pScreen = SDL_SetVideoMode(width, height, bpp, flags);
m_bFullscreen = fullscreen;
m_bRunning = true;
printf("Game Initialised Successfully\n");
// TEST GRAPHICS INIT
testSprite = NULL;
testSprite = SDL_Graphics::Load("testimg.bmp");
}
void Game::HandleEvents(Game* game)
{
SDL_Event event;
if(SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
game->Quit();
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_ESCAPE:
game->Quit();
}
break;
}
}
}
void Game::Update()
{
}
void Game::Draw()
{
SDL_Graphics::Draw(m_pScreen, testSprite, 300, 300, 0, 0, 50,50);
SDL_Flip(m_pScreen);
}
void Game::Clean()
{
}
int main(int argc, char* argv[])
{
Game game;
game.Init("test",640,480,32,false);
while(game.Running())
{
game.HandleEvents(&game);
game.Update();
game.Draw();
}
game.Clean();
return 0;
}
//SDL_Graphics.h
#ifndef _SDL_GRAPHICS_H_
#define _SDL_GRAPHICS_H_
#include <SDL.h>
class SDL_Graphics
{
public:
SDL_Graphics();
static SDL_Surface* Load(char* File);
static bool Draw(SDL_Surface* dest, SDL_Surface* src, int x, int y);
static bool Draw(SDL_Surface* dest, SDL_Surface* src, int x, int y, int x2,
int y2, int width, int height);
};
#endif
//SDL_Graphics.cpp
#include "SDL_Graphics.h"
SDL_Surface* SDL_Graphics::Load(char* File)
{
SDL_Surface* temp = NULL;
SDL_Surface* optimized = NULL;
if((temp = SDL_LoadBMP(File)) == NULL)
{
return NULL;
}
optimized = SDL_DisplayFormatAlpha(temp);
SDL_FreeSurface(temp);
return optimized;
}
bool SDL_Graphics::Draw(SDL_Surface* dest, SDL_Surface* src, int x, int y)
{
if(dest == NULL || src == NULL)
{
return false;
}
SDL_Rect destR;
destR.x = x;
destR.y = y;
SDL_BlitSurface(src, NULL, dest, &destR);
return true;
}
bool SDL_Graphics::Draw(SDL_Surface* dest, SDL_Surface* src, int x, int y, int x2, int y2, int width, int height) {
if(dest == NULL || src == NULL) {
return false;
}
SDL_Rect destR;
destR.x = x;
destR.y = y;
SDL_Rect srcR;
srcR.x = x2;
srcR.y = y2;
srcR.w = width;
srcR.h = height;
SDL_BlitSurface(src, &srcR, dest, &destR);
return true;
}