#include <windows.h>
#include <cstdlib>
#include <sdl/sdl.h>
inline void Msg( char* const str, char* const info = "" )
{
MessageBox( NULL, str, info, MB_OK );
}
SDL_Surface* G_pScreen = NULL;
SDL_Surface* G_pImage = NULL;
SDL_Surface* G_pBg = NULL;
int G_xPos = 0;
int G_yPos = 0;
bool LoadImage( char* const path, SDL_Surface* screen );
bool Init( Uint32 flags = SDL_INIT_VIDEO, int width = 640, int height = 480,
int bpp = 32, Uint32 mode = SDL_HWSURFACE|SDL_DOUBLEBUF )
{
if( SDL_Init( flags ) < 0 )
return false;
if( !( G_pScreen = SDL_SetVideoMode( width, height, bpp, mode ) ) )
return false;
if( !LoadImage( "J:\\Test\\ship.bmp", G_pImage ) )
return false;
if( !LoadImage( "J:\\Test\\bg.bmp", G_pBg ) )
return false;
std::atexit( SDL_Quit );
return true;
}
bool LoadImage( char* const path, SDL_Surface* screen )
{
if( !( screen = SDL_LoadBMP( path ) ) )
return false;
return true;
}
bool DrawImage( SDL_Surface* const src, SDL_Surface* dest, int x, int y )
{
SDL_Rect start;
start.x = 0;
start.y = 0;
if( SDL_BlitSurface( G_pBg, NULL, dest, &start ) == -1 )
return false;
return true;
}
bool DrawImage( SDL_Surface* const src, SDL_Surface* dest, int x, int y,
int x2, int y2, int width, int height )
{
SDL_Rect start;
start.x = x;
start.y = y;
SDL_Rect start2;
start2.x = x2;
start2.y = y2;
start2.w = width;
start2.h = height;
if( SDL_BlitSurface( src, &start2, dest, &start ) == -1 )
return false;
return true;
}
void DrawScene( SDL_Surface* const screen )
{
DrawImage( G_pBg, G_pScreen, G_xPos - 2, G_yPos - 2,
G_xPos - 2, G_yPos - 2, 54, 54 );
DrawImage( G_pImage, G_pScreen, G_xPos, G_yPos );
SDL_Flip( screen );
}
int main(int argc, char *argv[])
{
if( !Init() )
Msg( "Error on init", "Error" );
DrawImage( G_pBg, G_pScreen, 0, 0 );
Uint8* keys;
bool done = false;
while( !done )
{
SDL_Event event;
while( SDL_PollEvent( &event ) )
{
if( event.type == SDL_QUIT )
done = true;
if( event.type == SDL_KEYDOWN )
if( event.key.keysym.sym == SDLK_ESCAPE )
done = true;
}
keys = SDL_GetKeyState( NULL );
if( keys[ SDLK_UP ] )
++G_yPos;
if( keys[ SDLK_DOWN ] )
--G_yPos;
if( keys[ SDLK_LEFT ] )
--G_xPos;
if( keys[ SDLK_RIGHT ] )
++G_xPos;
DrawScene( G_pScreen );
}
return 0;
}
Error in SDL_BlitSurface
I'm new to SDL and so I started with the Cone3D SDL tutorial2 (though I didn't copy it exactly, but the program is still doing the same or should).
But I have an error in every call to SDL_BlitSurface and I don't know why.
The images are loaded successfully and everything else is also initialized successfully.
But still SDL_Blitsurface (in those two DrawImage functions) returns allways -1
From my point of view everything should be fine, maybe you can look over my code? Thank you :)
On the first DrawImage function, you appear to be passing *const src, but instead of using that you've hardcoded in g_pBg instead of passing in anything as a parameter.
The second one looks okay, comparing it with the original Cone3D example. Try taking out const and just have SDL_Surface *src.
I'm using SDL too, but I'm doing things differently.
I hope that helps, let me know if it still doesn't work.
ukdeveloper.
The second one looks okay, comparing it with the original Cone3D example. Try taking out const and just have SDL_Surface *src.
I'm using SDL too, but I'm doing things differently.
I hope that helps, let me know if it still doesn't work.
ukdeveloper.
I hardcoded G_pBg only for testing purposes, but it the error ist still there.
I removed the const from those parameters but there is still the same problem.
Can you post me a little example code that's running on your machine?
I removed the const from those parameters but there is still the same problem.
Can you post me a little example code that's running on your machine?
Here's what I've been using for Pong. It's part of a class, but it should work for you if you take away the whole class bit:
And you can use it by:
To be honest, I think that this approach is better than the Cone3D version, but that's just my opinion.
Try these tutorials - they're what my function is based on:
Our very own Rob Loach
Andrew's Home Page
I've been using SDL for about a month now, and Cone3D still doesn't make a lot of sense. Those ones are easier.
Good luck,
ukdeveloper.
void SpriteWrapper::Draw(SDL_Surface *surface,SDL_Surface *screen,int x,int y){ SDL_Rect sprite; sprite.x=x; sprite.y=y; SDL_BlitSurface(surface,NULL,screen,&sprite); }
And you can use it by:
paddle1.Draw(paddle_1,GameWindow,75,250); // Take away the paddle1. bit// paddle_1 is an SDL_Surface that you loaded earlier. //GameWindow is the name I gave to my SDL_Surface which was the window loaded (with SDL_HWSURFACE etc).//75 is the x co-ordinate of the destination, 250 is the y co-ordinate
To be honest, I think that this approach is better than the Cone3D version, but that's just my opinion.
Try these tutorials - they're what my function is based on:
Our very own Rob Loach
Andrew's Home Page
I've been using SDL for about a month now, and Cone3D still doesn't make a lot of sense. Those ones are easier.
Good luck,
ukdeveloper.
Well...your draw function doesn't work, too :(
I'll try those two tutorials and I hope I'll find the error :)
/edit
The example from Rob Loach works fine :)
I'll try those two tutorials and I hope I'll find the error :)
/edit
The example from Rob Loach works fine :)
Before blitting you might want to call SDL_LockSurface, because you are using HW_SURFACE. Then SDL_UnlockSurface, when the drawing operation is done.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement