sdl wont draw

Started by
4 comments, last by raptorstrike 19 years, 7 months ago
ok is there somthing im forgeting to initialize here beacause when i go to draw a picture it dosnt show heres my source [help]

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#include <libiberty.h>
#include "Player.h"
#include "GameEngine.h"
#include "SDL.h"


/* The screen surface */


 SDL_Surface *screen = NULL;
 
/* This function draws to the screen; replace this with your own code! */
static void
draw ()
{
 Player *a;
 a = new Player("Player.bmp");
 GameEngine  *pGame;
 pGame = new GameEngine(); 
 a->SetImage("Player.bmp", a->GetImage());
 int b = 250;
 int c = 24;
 int d = 32;
 int e = 1;
 
 //pGame->DrawIMG(a->GetImage(),screen, 250,250,24,32,1,1);
  SDL_Rect dest;
  dest.x = 1;
  dest.y = 1;
  SDL_Rect dest2;
  dest2.x = 1;
  dest2.y = 1;
  dest2.w = 24;
  dest2.h = 32;
  SDL_BlitSurface(a->GetImage(), &dest2, screen, &dest);
  SDL_Flip(screen);

}; 


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

    /* Initialize SDL */
    if (SDL_Init (SDL_INIT_VIDEO) < 0)
    {
        asprintf (&msg, "Couldn't initialize SDL: %s\n", SDL_GetError ());
        MessageBox (0, msg, "Error", MB_ICONHAND);
        free (msg);
        exit (1);
    }
    atexit (SDL_Quit);

    /* Set 640x480 16-bits video mode */
    screen = SDL_SetVideoMode (640, 480, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        asprintf (&msg, "Couldn't set 640x480x16 video mode: %s\n",
          SDL_GetError ());
        MessageBox (0, msg, "Error", MB_ICONHAND);
        free (msg);
        exit (2);
    }
    SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);

    done = 0;
    while (!done)
    {
        SDL_Event event;

        /* Check for events */
        while (SDL_PollEvent (&event))
        {
            switch (event.type)
            {
            case SDL_KEYDOWN:
                break;
            case SDL_QUIT:
                done = 1;
                break;
            default:
                break;
            }
        }

        /* Draw to screen */
        draw ();
    }

    return 0;
}







Player::Player(const char* FILE)
 // allocate memory for all variables
{
    Image = NULL;
    m_direction = 0;
    m_Row = 1;
    m_Col = 1; 
    posX = 0;
    posY = 0;
    Create(FILE,Image);
}
BOOL Player::Create(const char* szFileName,SDL_Surface *image)
{
    image = SDL_LoadBMP(szFileName);
    if(szFileName = NULL)
    {
        printf("Unable to load %s\n because it dostn exist",SDL_GetError());
        return false;
    };
    return true;
}







SDL_Surface* Player::SetImage(const char *szNewFile,SDL_Surface *Image)
{
    szFileName = szNewFile;
    Image = SDL_LoadBMP(szFileName);
};    






that is the main file (main.cpp) and all the self made functions called in the main file if you need more info let me know (and dont just say i need more info tell me what) ok thanks for your help[grin] [Edited by - raptorstrike on September 28, 2004 7:56:27 PM]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Advertisement
try changing

  SDL_Rect dest;  dest.x = 1;  dest.y = 1;  SDL_Rect dest2;  dest2.x = 1;  dest2.y = 1;  dest2.w = 24;  dest2.h = 32;


to

  SDL_Rect dest;  dest.x = 1;  dest.y = 1;  dest.w = 24;  dest.h = 32;  SDL_Rect dest2;  dest2.x = 1;  dest2.y = 1;  dest2.w = 24;  dest2.h = 32;


You should rename one of those "dest" to "source" to make the code more readable.
no it still dosnt work its really ticking me off i dont see whats wrong
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
K, first off I want to point out that it's useless to declare the SDL_DOUBLEBUF flag with the SDL_SWSURFACE flag - the two are incompatible. Double buffering only works with hardware surfaces (SDL_HWSURFACE). This doesn't affect anything outright, since SDL_Flip() will just call SDL_UpdateRect() rather than flipping the buffers, but just FYI.

Now then, I have a hypothesis as to what your problem is. This function call:
a->SetImage("Player.bmp", a->GetImage());

Is kind of confusing. First of all, you're setting an internal data member of the class, so why do you have to pass in the value that will ultimately be set in the function?
SDL_Surface* Player::SetImage(const char *szNewFile,SDL_Surface *Image){    szFileName = szNewFile;    Image = SDL_LoadBMP(szFileName);}; 

This isn't good. Are you setting CPlayer::Image or the local definition of Image? I'm not 100% sure, but I think you're setting the local definition of Image (defined in the arguments list) rather than CPlayer::Image. In that case, when you call this:
SDL_BlitSurface(a->GetImage(), &dest2, screen, &dest);

You're passing in a null value for the srcrect. This won't cause an error, but it will end up blitting precisely squat.

You can take out the second parameter of both CPlayer::Create and CPlayer::SetImage. You should be setting the internal data member CPlayer::Image anyways.

Oh yea, and you have a huge memory leak in your draw() function. You're allocating memory every single frame but not releasing it. Not good. If you're going to create new instances of CPlayer and GameEngine every frame (not recommended - one instance at the beginning of the app will suffice, for GameEngine anyways), at least call delete at some point to get rid of them.

Drew Sikora
Executive Producer
GameDev.net

Gaiiden's right on this one. I'm not even going to comment on how to make SetImage work the way you are trying to use it. Instead, I suggest an alternative solution: completely hide that image member variable. Whenever possible, try and avoid get and set methods for variables unless it 'really makes sense'. Try to think of the overall operation you are accomplishing by using the accessors and modifiers. It may be possible to wrap that operation up in a higher level function that just takes some parameters and does all the member variable fiddling for you.

Also, I don't know anything about SDL, so this may not be a problem. Usually, though, you don't want to be re-loading files off disk every frame. Currently your draw() function indirectly invokes SDL_LoadBMP. You probably want to create the player instance earlier and load the bitmap at that time. The bitmap used for the player should probably be passed into the Player class's constructor. The drawing functionality should probably be exposed via a simple Player::Draw() function, which handles the rect calculation and all that hooplah internally.

Good luck!
- Jason Citron- Programmer, Stormfront Studios- www.stormfront.com
thanks alot you guys i used a hard coded Surface and it worked just fine (i have alot of bugs to work out in my engine (its my first one [oh]))
[grin]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie

This topic is closed to new replies.

Advertisement