Problem with surface

I would like show my code to you.
I have a problem, I can’t show my images and I don’t understand why.
tnx

**** File name: game.h ****
#ifndef SDL_H
#define SDL_H
#include “SDL.h”
#endif
//////////////////////////////////////////////////////////////////////

#ifndef GAME_H
#define GAME_H
class Game
{
protected:
SDL_Surface *screen;

public:
Game(Uint32);
Game();
~Game();

public:
int videomode(int,int,int,Uint32,char*,char*,const char*);
void upall(void);
};
#endif
//////////////////////////////////////////////////////////////////////

**** File name: game.cc ****

#include <stdlib.h>
#include “game.h”
//////////////////////////////////////////////////////////////////////

Game::Game()
{
}

//

Game::Game(Uint32 flag)
{
if (SDL_Init(flag) < 0)
fprintf(stderr, “game.cc function Game():\n\t\t%s\n”,
SDL_GetError());

atexit(SDL_Quit);
}

//
Game::~Game()
{
SDL_Quit();
}

//

int Game::videomode(int w, int h, int bpp, Uint32 flags, char* wint,
char* icot, const char* icof)
{
if ( (screen = SDL_SetVideoMode(w,h,bpp,flags) ) == NULL )
{
fprintf(stderr, “game.cc fucntion videomode():\n\t\t%s\n”,
SDL_GetError());
return 1;
}

SDL_WM_SetCaption(wint, icot);

if (icof != NULL)
SDL_WM_SetIcon(SDL_LoadBMP(icof), NULL);
}

//

void Game::upall(void)
{
SDL_UpdateRect(screen,0,0,0,0);
}
//////////////////////////////////////////////////////////////////////

**** File name: image.h ****

#ifndef GAME_H
#define GAME_H
#include “game.h”
#endif
//////////////////////////////////////////////////////////////////////

#ifndef IMAGE_H
#define IMAGE_H
class Image : public Game
{
public:
SDL_Surface *image;

public:
int load(const char* file);
void show(int sx, int sy, int sw, int sh, int dx, int dy, int dw,
int dh);
void remove(void);
};
#endif
//////////////////////////////////////////////////////////////////////

**** File name: image.cc ****

#include <stdlib.h>
#include “game.h”
#include “image.h”
//////////////////////////////////////////////////////////////////////

int Image::load(const char *file_name)
{
if ( (image = SDL_LoadBMP(file_name)) == NULL )
{
fprintf(stderr, “image.cc function load():\n\t\t%s\n”,
SDL_GetError());
return 1;
}

return 0;
}

//

void Image::show(int sx, int sy, int sw, int sh, int dx, int dy, int dw,
int dh)
{
SDL_Rect src, dst;

src.x = sx;
src.y = sy;
src.w = sw;
src.h = sh;
dst.x = dx;
dst.y = dy;
dst.w = dw;
dst.h = dh;

SDL_BlitSurface(image, &src, screen, &dst);
}

//

void Image::remove(void)
{
SDL_FreeSurface(image);
}
//////////////////////////////////////////////////////////////////////

**** File name: main.cc ****

#include “game.h”
#include “image.h”

int main(int argc, char* argv[])
{
bool quit = 0;

Game game(SDL_INIT_VIDEO);

game.videomode(640,480,16,SDL_HWSURFACE|SDL_DOUBLEBUF,“prova”,“prova”,NULL);

/*
while(!quit)
{
}
*/

Image bg;
bg.load(“background.bmp”);
bg.show(0,0,bg.image->w,bg.image->h,0,0,bg.image->w,bg.image->h);

Image main;
main.load(“main.bmp”);

main.show(0,0,main.image->w,main.image->h,0,0,main.image->w,main.image->h);

game.upall();

SDL_Delay(3000);

main.remove();
bg.remove();

return 0;
}

Tnx for your support
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20031008/d2e06268/attachment.pgp

— NighTiger wrote: > I would like show my
code to you.

I have a problem, I can’t show my images and I don’t understand why.
tnx

This is wrong:

**** File name: game.h ****
#ifndef SDL_H
#define SDL_H
#include “SDL.h”
#endif

//////////////////////////////////////////////////////////////////////

It is up to you to prevent multiple inclusions of your own headers, but
it is up to SDL to prevent multiple inclusion of it’s own headers.
Just

#include “SDL.h”

will suffice.

This #ifndef - #endif pair is correct, because game.h is your own
header.

#ifndef GAME_H
#define GAME_H
class Game
{
protected:
SDL_Surface *screen;

public:
Game(Uint32);
Game();
~Game();

public:
int videomode(int,int,int,Uint32,char*,char*,const char*);
void upall(void);
};
#endif

//////////////////////////////////////////////////////////////////////

**** File name: game.cc ****

#include <stdlib.h>
#include “game.h”

//////////////////////////////////////////////////////////////////////

Game::Game()
{
}

//

Game::Game(Uint32 flag)
{
if (SDL_Init(flag) < 0)
fprintf(stderr, “game.cc function Game():\n\t\t%s\n”,
SDL_GetError());

I believe if the SDL_Init fails here, you should gracefully quit.
There’s no way of returning a value from a constructor, so I do’nt
actually know the safest way to do that, but your game depends on SDL
so if SDL can’t init you’re in trouble.

atexit(SDL_Quit);

There is no need for this atexit() call, as you already have a call to
SDL_Quit() (correctly) in the Game destructor.

}

//
Game::~Game()
{
SDL_Quit();
}

//

int Game::videomode(int w, int h, int bpp, Uint32 flags, char* wint,
char* icot, const char* icof)
{
if ( (screen = SDL_SetVideoMode(w,h,bpp,flags) ) == NULL )
{
fprintf(stderr, “game.cc fucntion videomode():\n\t\t%s\n”,
SDL_GetError());
return 1;
}

SDL_WM_SetCaption(wint, icot);

if (icof != NULL)
SDL_WM_SetIcon(SDL_LoadBMP(icof), NULL);
}

//

void Game::upall(void)
{
SDL_UpdateRect(screen,0,0,0,0);

I’m pretty sure you should call SDL_Flip() here, as you’re requesting a
double buffered hardware surface.

}

//////////////////////////////////////////////////////////////////////

These inclusion guards are also wrong. The only place you should see
#ifndef GAME_H guards is inside GAME_H. WE’re inside image.h so you
only need the #ifndef IMAGE_H guard.

**** File name: image.h ****

#ifndef GAME_H
#define GAME_H
#include “game.h”
#endif

Change the above to just

#include “game.h”

//////////////////////////////////////////////////////////////////////

#ifndef IMAGE_H
#define IMAGE_H
class Image : public Game

I think this is poor design. There’s no reason for Image to be derived
from game, except to share the SDL_Surface* screen. You should have a
public function in Game to get hold of the screen, not derive like
this.

You could probably make that function static too.

{
public:
SDL_Surface *image;

public:
int load(const char* file);
void show(int sx, int sy, int sw, int sh, int dx, int dy, int dw,
int dh);
void remove(void);
};
#endif

//////////////////////////////////////////////////////////////////////

**** File name: image.cc ****

#include <stdlib.h>
#include “game.h”
#include “image.h”

//////////////////////////////////////////////////////////////////////

int Image::load(const char *file_name)
{
if ( (image = SDL_LoadBMP(file_name)) == NULL )
{
fprintf(stderr, “image.cc function load():\n\t\t%s\n”,
SDL_GetError());
return 1;
}

return 0;
}

//

void Image::show(int sx, int sy, int sw, int sh, int dx, int dy, int
dw,
int dh)
{
SDL_Rect src, dst;

src.x = sx;
src.y = sy;
src.w = sw;
src.h = sh;
dst.x = dx;
dst.y = dy;
dst.w = dw;
dst.h = dh;

SDL_BlitSurface(image, &src, screen, &dst);

HEre, instead of inheriting screen from Game, you should have a
function call which returns the screen pointer.

}

//

void Image::remove(void)
{
SDL_FreeSurface(image);
}

Shouldn’t this be in the Image destrutor?

//////////////////////////////////////////////////////////////////////

**** File name: main.cc ****

#include “game.h”
#include “image.h”

int main(int argc, char* argv)
{
bool quit = 0;

Game game(SDL_INIT_VIDEO);

game.videomode(640,480,16,SDL_HWSURFACE|SDL_DOUBLEBUF,“prova”,“prova”,NULL);

/*
while(!quit)
{
}
*/

Don’t include commented out code when you send it to mailing lists :slight_smile:

Image bg;
bg.load(“background.bmp”);
bg.show(0,0,bg.image->w,bg.image->h,0,0,bg.image->w,bg.image->h);

Image main;
main.load(“main.bmp”);

main.show(0,0,main.image->w,main.image->h,0,0,main.image->w,main.image->h);

game.upall();

SDL_Delay(3000);

main.remove();
bg.remove();

You shouldn’t need these remove() calls - the code for remove() should
be in the Image destructor which will be automatically called.

return 0;
}

The one which might be stopping the images showing is the UpdateREcts
instead of FLip, but there could be other issues. You load the BMP
files but you don’t tell me what type they are. You’re setting the
screen to a 16bpp mode (wisely!) but are the BMP files 16bpp? IF so,
no problem, but if they’re anythign else you should really call
SDL_Displayformat on them, adn if they’re 8bpp then you need to make
sure that you’ve actually loaded their palette in properly (I 'm pretty
sure Load_BMP doesn’t load the palette for you, though I may be wrong)

Hope some of this criticism helps.________________________________________________________________________
Want to chat instantly with your online friends? Get the FREE Yahoo!
Messenger http://mail.messenger.yahoo.co.uk