6 Replies - 721 Views - Last Post: 27 April 2012 - 04:37 PM Rate Topic: -----

#1 Tom_Leonardsson  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 07-August 11

SDL problem, the BMP wont show!

Posted 25 April 2012 - 04:08 AM

Hi there could anyone help me with this
#include "sdl/sdl.h"


const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
const char* WINDOW_TITLE = "Le Shoot Em Up";

void drawSprite(SDL_Surface* imageSurface,
                SDL_Surface* screenSurface,
                int srcX, int srcY,
                int dstX, int dstY,
                int width, int height);

int main(int argc, char **argv)
{
   SDL_Init( SDL_INIT_VIDEO );

   SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0,
      SDL_HWSURFACE | SDL_DOUBLEBUF );
   SDL_WM_SetCaption( WINDOW_TITLE, 0 );

   SDL_Surface* bitmap = SDL_LoadBMP("sprite.bmp");
    SDL_SetColorKey(bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 200, 0, 200));

   int spriteImageX = 24;
   int spriteImageY = 63;
   int spriteWidth = 100;
   int spriteHeight = 100;


   int spriteX = 50;
   int spriteY = 50;

   SDL_Event event;
   bool gameRunning = true;

   bool keysHeld[323] = {false}; // everything will be initialized to false

   while (gameRunning)
   {
      // Handle input
      if (SDL_PollEvent(&event))
      {
         if (event.type == SDL_QUIT)
         {
            gameRunning = false;
         }

         if (event.type == SDL_KEYDOWN)
         {
            keysHeld[event.key.keysym.sym] = true;
         }
         if (event.type == SDL_KEYUP)
         {
            keysHeld[event.key.keysym.sym] = false;
         }
      }

      if ( keysHeld[SDLK_ESCAPE] )
      {
         gameRunning = false;
      }

      if ( keysHeld[SDLK_LEFT] )
      {
         spriteX -= 1;
      }
      if ( keysHeld[SDLK_RIGHT] )
      {
         spriteX += 1;
      }
      if ( keysHeld[SDLK_UP] )
      {
         spriteY -= 1;
      }
      if (keysHeld[SDLK_DOWN])
      {
         spriteY += 1;
      }

      // Draw the scene
      SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));

      drawSprite(bitmap,
                 screen,
                 spriteImageX, spriteImageY,
                 spriteX, spriteY,
                 spriteWidth, spriteHeight);

      SDL_Flip(screen);
   }

   SDL_FreeSurface(bitmap);

   SDL_Quit();

   return 0;
}

void drawSprite(SDL_Surface* imageSurface,
                SDL_Surface* screenSurface,
                int srcX, int srcY,
                int dstX, int dstY,
                int width, int height)
{
   SDL_Rect srcRect;
   srcRect.x = srcX;
   srcRect.y = srcY;
   srcRect.w = width;
   srcRect.h = height;

   SDL_Rect dstRect;
   dstRect.x = dstX;
   dstRect.y = dstY;
   dstRect.w = width;
   dstRect.h = height;

   SDL_BlitSurface(imageSurface, &srcRect, screenSurface, &dstRect);
}




thanks

This post has been edited by Tom_Leonardsson: 25 April 2012 - 04:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: SDL problem, the BMP wont show!

#2 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1326
  • View blog
  • Posts: 4,551
  • Joined: 19-February 09

Re: SDL problem, the BMP wont show!

Posted 25 April 2012 - 09:25 PM

It seems ok. Have you put the sprite.bmp file in the same folder as the executable program?
Was This Post Helpful? 0
  • +
  • -

#3 Tom_Leonardsson  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 07-August 11

Re: SDL problem, the BMP wont show!

Posted 26 April 2012 - 12:30 AM

Hi, I have a problem there is suppose to be a enemy on the screen but he wont show, I belive it's something wrong with the code. Iam quite new to this so dont flame.
Thanks!


#include "sdl/sdl.h"
const int WINDOW_WIDTH = 600;
const int WINDOW_HEIGHT = 480;
const char* WINDOW_TITLE = " Drago  ns In Space ";
void drawSprite(SDL_Surface* imageSurface,
                SDL_Surface* screenSurface,
                int srcX, int srcY,
                int dstX, int dstY,
                int width, int height);
int main(int argc, char **argv)
{
   SDL_Init( SDL_INIT_VIDEO );
   SDL_Surface* hello = NULL;
   SDL_Surface* enemy = NULL;
   SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0,
      SDL_HWSURFACE | SDL_DOUBLEBUF /*| SDL_FULLSCREEN*/);
   SDL_WM_SetCaption( WINDOW_TITLE, 0 );
   // Images
   SDL_Surface* bitmap = SDL_LoadBMP( "sprite.bmp" );
   hello = SDL_LoadBMP( "hello.bmp" );
   enemy = SDL_LoadBMP( "enemy.bmp" );
   // Decides what color shall be removed
   SDL_SetColorKey(bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255));
   //---ints---//
   int spriteImageX = 24;
   int spriteImageY = 63;
   int spriteWidth = 65;
   int spriteHeight = 44;
   int spriteX = 100;
   int spriteY = 100;
   int enemyImageX = 312;
   int enemyImageY = 231;
   int enemyWidth = 65;
   int enemyHeight = 44;
   int enemyX = 100;
   int enemyY = 100;
   SDL_Event event;
   bool gameRunning = true;
   bool keysHeld[323] = {false};
   while (gameRunning)
   {

      if (SDL_PollEvent(&event))
      {
         if (event.type == SDL_QUIT)
         {
            gameRunning = false;
         }
        // keyinput
         if (event.type == SDL_KEYDOWN)
         {
            keysHeld[event.key.keysym.sym] = true;
         }
         if (event.type == SDL_KEYUP)
         {
            keysHeld[event.key.keysym.sym] = false;
         }
      }

      if ( keysHeld[SDLK_ESCAPE] )
      {
         gameRunning = false;
      }

      if ( keysHeld[SDLK_a] )
      {
         spriteX -= 1;
      }
      if ( keysHeld[SDLK_d] )
      {
         spriteX += 1;
      }
      if ( keysHeld[SDLK_w] )
      {
         spriteY -= 1;
      }
      if (keysHeld[SDLK_s])
      {
         spriteY += 1;
      }
      if (keysHeld[SDLK_k])
      {
         spriteX += 5;
      }
      if (keysHeld[SDLK_j])
      {
            spriteX -= 5;


      }
       if (keysHeld[SDLK_t])
      {
            spriteX = 100;
            spriteY = 100;
            // this is what you use to animate of sort
            //spriteImageX = 132;
            //spriteImageY = 92;
      }
      SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
      drawSprite(bitmap,
                 screen,
                 spriteImageX, spriteImageY,
                 spriteX, spriteY,
                 spriteWidth, spriteHeight);
     SDL_Flip(screen);

      SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
      drawSprite(bitmap,
                 screen,
                 enemyImageX, enemyImageY,
                 enemyX, enemyY,
                 enemyWidth, enemyHeight);
     //SDL_Flip(screen);


   }
   SDL_FreeSurface( hello );
   SDL_FreeSurface(bitmap);
   SDL_Quit();
   return 0;
}
void drawSprite(SDL_Surface* imageSurface,
                SDL_Surface* screenSurface,
                int srcX, int srcY,
                int dstX, int dstY,
                int width, int height)
{
   SDL_Rect srcRect;
   srcRect.x = srcX;
   srcRect.y = srcY;
   srcRect.w = width;
   srcRect.h = height;
   SDL_Rect dstRect;
   dstRect.x = dstX;
   dstRect.y = dstY;
   dstRect.w = width;
   dstRect.h = height;
   SDL_BlitSurface(imageSurface, &srcRect, screenSurface, &dstRect);
}


Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6049
  • View blog
  • Posts: 23,475
  • Joined: 23-August 08

Re: SDL problem, the BMP wont show!

Posted 26 April 2012 - 03:14 AM

Merged duplicate topics. Do NOT create a new topic on the same subject as one you already have!
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: SDL problem, the BMP wont show!

Posted 26 April 2012 - 06:53 AM

Does your program build with 0 errors?

Do you have the image file (sprite.bmp) in your project directory?
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: SDL problem, the BMP wont show!

Posted 27 April 2012 - 02:23 PM

you are not checking return values in your code. It would really help if you review documentation and amend your code accordingly.
Was This Post Helpful? 0
  • +
  • -

#7 reaper4334  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 107
  • Joined: 01-November 09

Re: SDL problem, the BMP wont show!

Posted 27 April 2012 - 04:37 PM

As ButchDean said, you should be checking return values. It will help narrow down the problem very much.

Also, as a few other people mentioned, ensure that you image is in the correct directory.
If you're using Visual Studio, it must be in the project directory (usually the same place as your .cpp files). If you're not using Visual Studio, it should probably be in the same directory as the .exe file.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1