SDL_GFX library use!

Started by
10 comments, last by samuraicrow 16 years, 11 months ago
Hey! Ok so i sorted the gfx library issue, now my solution dies on this line unsigned int colour = SDL_MapRGB(TowerDefence.window->format,255,0,0); Any idea why? TowerDefence is a global instance of an SDL surface! Thanks Andy [Edited by - Renegadeandy on May 21, 2007 7:02:02 AM]
Advertisement
How are you creating that surface, and are you checking that said creation succeeds?
, this is my surface setup:

// Tower Defence
// master.cpp
// Started May 19th 2007, 17:47
// Malcolm Buchanan

#include "master.h"

// master::start_sdl()
// Returns true if SDL has been initialised okay; else return false for program termination
bool master::start_sdl()
{
if(SDL_Init(SDL_INIT_EVERYTHING)==-1) // If we can't initialise SDL for whatever reason...
{
std::cout<<"master::start_sdl: FAILED ( "<<SDL_GetError()<<"\n"; // Temporary; this will evolve into a full logging system at some point
SDL_Quit();
return (false);
}else{
std::cout<<"master::start_sdl(): SUCCESS\n";
return (true);
}
}


// master::create_hwnd(std::string title)
// Creates our SDL window with the given title, and sets it fullscreen
bool master::create_hwnd()
{
SDL_ShowCursor(0);
window=SDL_SetVideoMode(1024,768,32,SDL_SWSURFACE|SDL_FULLSCREEN);
SDL_WM_SetCaption("Tower Defence",NULL);
if(window==NULL)
{
std::cout<<"Error occurred in master::create_hwnd(): "<<SDL_GetError()<<"\n"; // Again, this will evolve into our logging system
SDL_Quit();
return(false);
}else{
std::cout<<"master::create_hwnd(): SUCCESS\n";
set_running(1);
return(true);
}
}


// master::set_running(int flag)
// If we pass in 1, running gets set to true; 0 means it gets set to false
void master::set_running(int flag)
{
switch(flag)
{
case 1:
running=true;
std::cout<<"master::set_running(int flag): "<<flag<<" was passed in, and running is now TRUE\n";
break;
case 0:
running=false;
std::cout<<"master::set_running(int flag): "<<flag<<" was passed in, and running is now FALSE\n";
exit(0);
break;
default:
std::cout<<"Unexpected value for flag in master::set_running(int flag); exiting\n";
running=false;
SDL_Quit();
exit(-1);
}
}


// master::is_running()
// Returns true if the program's running, or false if it isn't
bool master::is_running()
{
switch(running)
{
case true:
return(true);
break;
case false:
return(false);
break;
}
return (true);
}


// master::closedown()
// Closes the program and stops everything; it'll break our while loop in main which will cause termination
void master::closedown()
{
SDL_FreeSurface(window);
SDL_Quit();
set_running(0);
}


and this is the globals.h

// Tower Defence
// globals.h
// Started 19th May 2007, 20:01
// Malcolm Buchanan

#ifndef _GLOBALS_H
#define _GLOBALS_H

#include "master.h"

extern master TowerDefence;

#endif

So that is how it is setup!
But that code never actually creates an instance of TowerDefence, or calls any of those functions - where do you do all that in your program?
yeah im sorry - its in here:

// Tower Defence
// main.cpp
// Started 19th May 2007, 13:48
// Malcolm Buchanan
#include <iostream>
#include "master.h"
#include "globals.h"
#include "enemy.h"

master TowerDefence;
SDL_Event testEvent;

void checkEvents();

int main(int argc, char *argv[]){

unsigned int colour = SDL_MapRGB(TowerDefence.window->format,255,0,0);


// Enemy *jim = new Enemy("Jim",1000,50,"grag",50,50,colour);

if(TowerDefence.start_sdl())
{
TowerDefence.create_hwnd();
}

while(TowerDefence.is_running())
{
checkEvents();
SDL_Flip(TowerDefence.window); // This will flip the surface with all the updated movement information
SDL_Delay(1);
}

return(0);
}


void checkEvents()
{
if(SDL_PollEvent(&testEvent))
{
if(testEvent.type==SDL_KEYDOWN)
{
switch(testEvent.key.keysym.sym)
{
case SDLK_ESCAPE:
TowerDefence.closedown();
break;
}
}
}
}
Quote:unsigned int colour = SDL_MapRGB(TowerDefence.window->format,255,0,0);


// Enemy *jim = new Enemy("Jim",1000,50,"grag",50,50,colour);

if(TowerDefence.start_sdl())
{
TowerDefence.create_hwnd();
}


You are trying to reference the format of the surface before it is created. Move the SDL_MapRGB line AFTER you have initialized the surface. Otherwise when you try to reference window->format window is still NULL or some arbitrary memory address.

[edit]
You may want to consider using the SDL types such as Uint32 as opposed to writing unsigned int. These are defined so that when you move to/from 16, 32, and 64 bit systems you know the size of the variables and return types. This is becoming an issue more and more because you may want to move your program to a 64 bit OS in the near future.
[/edit]

[Edited by - evillive2 on May 21, 2007 1:22:31 PM]
Evillive2
ahh thanks for the tip!

Right cracking now its going alright:

however now when i run this build of main.cpp :

// Tower Defence
// main.cpp
// Started 19th May 2007, 13:48
// Malcolm Buchanan
#include <iostream>
#include "master.h"
#include "globals.h"
#include "enemy.h"

master TowerDefence;
SDL_Event testEvent;

void checkEvents();

int main(int argc, char *argv[]){



if(TowerDefence.start_sdl())
{
TowerDefence.create_hwnd(); //create window
}


unsigned int colour = SDL_MapRGB(TowerDefence.window->format,255,0,0);


Enemy *jim = new Enemy("Jim",1000,50,"grag",50,50,colour);

while(TowerDefence.is_running())
{
checkEvents();
//jim->draw();
//jim->x = jim->x + 10;
SDL_Flip(TowerDefence.window);
SDL_Delay(1);
}
return (0);
}

void checkEvents()
{
if(SDL_PollEvent(&testEvent))
{
if(testEvent.type==SDL_KEYUP)
{
switch(testEvent.key.keysym.sym)
{
case SDLK_ESCAPE:
TowerDefence.closedown();
break;
}
}
}
}

It loads a blank screen - but when i try to press escape to quit out of the program it doesnt, and i have to hard restart my whole station, no alt tab, no ctrl alt delete, nothing!

Any help would be fantastic! I have tried SDL_KEYPRESS etc but KEYUP worked once, and never again since! :S

Andy
Quote:while(TowerDefence.is_running())
{
checkEvents();
//jim->draw();
//jim->x = jim->x + 10;
SDL_Flip(TowerDefence.window);
SDL_Delay(1);
}
return (0);
}


So far you are flipping a blank surface since jim->draw(); is commented out. Perhaps uncomment that and see if it draws anything.

Quote:but when i try to press escape to quit out of the program it doesnt...


Without seeing the TowerDefence.closedown() code and the TowerDefence.is_running() code I don't know what is going on there and cannot offer much help. You should however always handle the SDL_QUIT event which is what gets generated by clicking the close button on the window or you could run into loops like you were talking about.
Evillive2
If i dont make the window fullscreen , and instad make it resizable then it seems to work everytime!

Figured the draw out as well!

One question - i presume on each iteration of the game loop you are meant to clear the surface ready for a redraw - or what is the idea with that? If so how do you do that?

Thanks

Andy
Quote:One question - i presume on each iteration of the game loop you are meant to clear the surface ready for a redraw - or what is the idea with that? If so how do you do that?

That depends on if your drawing routines will take up the whole screen each time or not. If you were drawing a tilemap that takes up the entire screen for example then there is no need to clear the screen because the map is drawing over everything anyway. On the other hand, if it is a platform or space shooter where there may be a bunch of empty space, you could draw a black (or whatever color you want as a background) rectangle using SDL_FillRect(ptr_to_screen_surface,NULL,0) to fill the entire surface with a single color. Since this fills the screen it appears that you have cleared it.

Hope it helps.

p.s. did you try handling the SDL_QUIT event? Once that is done possibly try pushing the SDL_QUIT event onto the event queue using SDL_PushEvent in your TowerDefense.closedown() handler.
Evillive2

This topic is closed to new replies.

Advertisement