• Advertisement
Sign in to follow this  

SDL SDL_Surface problem

This topic is 2176 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there guys, ive just finished a few SDL tutorials but in every tutorial the writer never uses different .cpp files or headers, so i tried to create a simple 2 file game
main would create a instance of player which would just simply draw a image to screen and move around, but i cant seem to get it to work, the main problem is how should i get it to draw the player?
i under to uset he IMG_Load and the SDL_Blitsurface and what not,
but my problem is, if i create two SDL_Surfaces* one called image once called screen, how would i pass them into the player class?
and if i did pass them through how would i then draw it?

Here is some code i have written already
#include "SDL.h"
#include "SDL_image.h"
#include <string>
SDL_Surface *image = NULL;
SDL_Surface *drawImage = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;
class Player{
private:
std::string filename;
SDL_Event event;
public:
Player(SDL_Surface*);
void move();
void applySurfacePlayer(int x,int y,SDL_Surface* source,SDL_Surface* destination);
void draw();
};
Player::Player(SDL_Surface* image){
filename = ("dot.bmp");
image = IMG_Load(filename.c_str());
}
void Player::applySurfacePlayer(int x,int y,SDL_Surface* source,SDL_Surface* destination){
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source,NULL,destination,&offset);
}

void applySurface(int x,int y,SDL_Surface* source,SDL_Surface* destination){
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source,NULL,destination,&offset);
}
bool init(){
if (SDL_Init (SDL_INIT_EVERYTHING) == -1)
{
return false;
}
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
if (screen == NULL)
{
return false;
}
SDL_WM_SetCaption("Event Handling", NULL);
return true;
}
int main(int argc, char* args[] ){
bool quit = false;
Player gamePlayer(image);
//initialize SDL
if (init() == false)
{
return 1;
}
//applySurface(0,0,image,screen);

while (quit == false)
{
while (SDL_PollEvent( &event))
{
if (event.type == SDL_QUIT)
{
quit = true;
}
gamePlayer.applySurfacePlayer(0,0,image,screen);
}
if (SDL_Flip(screen) == -1)
{
return 1;
}
}
return 0;
}


Now this comes up with a black screen but why doesnt it draw the image? i give it the cords, i give it image which is set to dot.bmp, and screen is the window, any ideas on what i have done wrong or missed? also how can i get it to update? i mean i know how to make something move but i need the display of that object to update also,

Cheers for the help guys

Canvas

Share this post


Link to post
Share on other sites
Advertisement

Now this comes up with a black screen but why doesnt it draw the image?


Here, you're passing image by value, meaning when you set image to something else, it has no effect on the caller's image.


Player::Player(SDL_Surface* image){
filename = ("dot.bmp");
image = IMG_Load(filename.c_str()); //this has no effect on the caller's image
}


Try passing a reference to image instead. (though it's not good style for a constructor to have side effects)


Player::Player(SDL_Surface*& image){ //note the &
...



also how can i get it to update?


It will "update" whenever a new SDL event has occurred. For movement for example, you can keep track of the player's x,y position and update that position whenever a SDL_KEYDOWN event occurs. Then, you could call applySurfacePlayer with the new x,y.

Share this post


Link to post
Share on other sites
If i pass it a reference instead
Player::Player(SDL_Surface& image){
filename = ("dot.bmp");
image = IMG_Load(filename.c_str());
}

the image = has a red underline

its ok i did the &* but that just seems really weird, could i not make a display class that has a vector of SDL_Surfaces and i can pass that the image and cords to be drawn?

Share this post


Link to post
Share on other sites

could i not make a display class that has a vector of SDL_Surfaces and i can pass that the image and cords to be drawn?


You could do that. I was actually going to suggest with your current solution that you make image a member of player, so that way applySurfacePlayer would only take one parameter:


class Player
{
private:
std::string filename;
SDL_SURFACE* image;
SDL_Event event;
int x;
int y;

public:
Player();
void move();
void applySurfacePlayer(SDL_Surface* destination);
void draw();
};

Player::Player()
{
filename = ("dot.bmp");
image = IMG_Load(filename.c_str());
x = 0;
y = 0;
}

void Player::applySurfacePlayer(SDL_Surface* destination)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(image,NULL,destination,&offset);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement