2 Replies - 1199 Views - Last Post: 02 June 2014 - 02:46 AM Rate Topic: -----

#1 KG3UNFORGN  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 78
  • Joined: 19-October 10

SDL Example code SDL_LoadBMP() not working

Posted 21 May 2014 - 07:39 AM

Hey everyone I am new to SDL2 and I been trying to get this to work for the last hour. I am using the book SDL Game Development by Shawn Mitchell, but his code examples does not really match with his pictures. The LoadBMP code isn't working it's loading a blank screen.

Here is the source code.
Main.cpp
#include "game.h"

// Game Object
Game * g_game = 0;

int main(int argc, char* argv[])
{
	g_game = new Game();
	g_game->init("Chapter 1",100,100,640,480,SDL_WINDOW_SHOWN);

	while (g_game->running())
	{
		g_game->handleEvents();
		g_game->update();
		g_game->render();
	}

	g_game->clean();

	return 0;
}



Game.h
#ifndef __Game__
#define __Game__ 

#include <SDL.h>
#include <iostream>
using namespace std;

class Game
{

private:
	
	SDL_Window* gWindow;
	SDL_Renderer* gRenderer;

	SDL_Texture* gTexture;
	SDL_Rect gSourceRectangle;
	SDL_Rect gDestinationRectangle;




	bool gRunning;


public:
	Game() {}
	~Game() {}


	
	bool init(const char*, int, int,int,int,int);
	void render();
	void update();
	void handleEvents();
	void clean();

	bool running() { return gRunning;} 

};
#endif



Game.cpp
#include "game.h"


bool Game::init(const char* title, int xpos, int ypos, int width, int height, int flags)
{

	//Attempt to init SDL
	if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
	{

		cout << "SDL init SUCCESS!";
		// init the window
		gWindow = SDL_CreateWindow(title,xpos,ypos,width,height,flags);
		
		if(gWindow != 0) // Window init success
		{
			cout << "Window Creation Success\n";
			gRenderer = SDL_CreateRenderer(gWindow,-1,0);

			if(gRenderer != 0)
			{
				cout << "renderer creation success\n";
				SDL_SetRenderDrawColor(gRenderer,0,0,0,255);
		
			}
			else
			{
				cout << "Renderer fail\n";
				return false;
			}
		}
		else
		{
			cout << " Window init fail\n";
			return false;
		}
	}
		cout << "init success\n";
		gRunning = true;

                // Code here not working???
		SDL_Surface* pTempSurface = SDL_LoadBMP("Assets\avatar.bmp");
		gTexture =SDL_CreateTextureFromSurface(gRenderer,pTempSurface);
		SDL_FreeSurface(pTempSurface);

		SDL_QueryTexture(gTexture,NULL,NULL,&gSourceRectangle.w,&gSourceRectangle.h);
		gDestinationRectangle.x = gSourceRectangle.x = 0;
		gDestinationRectangle.y = gSourceRectangle.y = 0;
		gDestinationRectangle.w = gSourceRectangle.w;
		gDestinationRectangle.h = gSourceRectangle.h;


		return true;
}

void Game::render()
{

	SDL_RenderClear(gRenderer);
	SDL_RenderCopy(gRenderer,gTexture,&gSourceRectangle,&gDestinationRectangle);
	SDL_RenderPresent(gRenderer);
	
}

void Game::clean()
{
	cout << "Cleaning up the game\n";
	SDL_DestroyWindow(gWindow);
	SDL_DestroyRenderer(gRenderer);
	SDL_Quit();
	
}

void Game::handleEvents()
{
	SDL_Event event;
	if(SDL_PollEvent(&event))
	{
		switch (event.type)
		{
		case SDL_QUIT:
			gRunning = false;
			break;

		default:
			break;
		}
	}
}
void Game::update()
{


}



The Problem is in Game.cpp, in the init function. The book does not show where in the init code to place the draw image part, to init the image. It just says to throw it in there. So I placed it after it draws the window, it did not work, and I placed if after the init return's true. it still does not work. I am not sure what is happening, or how to fix it.

I placed my assets folder with my source code, like the book says and its not working still.

Is This A Good Question/Topic? 0
  • +

Replies To: SDL Example code SDL_LoadBMP() not working

#2 Peter O  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 181
  • Joined: 19-October 13

Re: SDL Example code SDL_LoadBMP() not working

Posted 21 May 2014 - 08:34 AM

Make sure the file path is correct. If you use backward slash in a string literal you have to escape it "Assets\\avatar.bmp" otherwise it will treat \a as some kind of special character. You could use a forward slash instead "Assets/avatar.bmp". Using forward slash is more cross-platform because it will work almost everywhere. Backwards slash only works on Windows as far as I know.
Was This Post Helpful? 0
  • +
  • -

#3 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 999
  • View blog
  • Posts: 4,177
  • Joined: 14-February 08

Re: SDL Example code SDL_LoadBMP() not working

Posted 02 June 2014 - 02:46 AM

You could use SDL_GetError() to find out what is going on.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1