Jump to content
  • Advertisement
Sign in to follow this  
Niux

SDL noob question - SDL_LoadBMP

This topic is 4828 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.. i just started with some SDL this morning, so im really new to it(thats why the post goes in here and not Alternative Game Libraries ) and ive bin trying to put an image in a window, following a tutorial. i got this
#include <iostream>
#include <stdlib.h>
#include <SDL/SDL.h>

using namespace std;

SDL_Surface *screen;

void draw(int x, int y, SDL_Surface *sprite); //sprite = the image to display

int main(int nArgs, char **args)
{
    SDL_Surface *image;
    bool isRunning;
    
    SDL_Init(SDL_INIT_VIDEO);
    atexit(SDL_Quit);
    
    screen = SDL_SetVideoMode(1024, 768, 32, SDL_HWSURFACE);
    image = SDL_LoadBMP("theImage.bmp");
    isRunning = true;
    while (isRunning)
    {
          SDL_Event event;
          SDL_PollEvent(&event);
          
         if(event.type == SDL_Quit)
         {
                       isRunning = false;
         }
         
       
          draw(0, 0, image);
          
          SDL_Flip(screen);
        
    }
    
    SDL_FreeSurface(image);
    
    return 0;
}

void draw(int x, int y, SDL_Surface *sprite)
{
     SDL_Rect source;
     SDL_Rect dest;
     
     source.x = 0;
     source.y = 0;
     source.h = sprite->h;
     source.w = sprite->w;
     
     dest.x = x;
     dest.y = y;
     dest.h = sprite->h;
     dest.w = sprite->w;
     
     SDL_BlitSurface(sprite, &source, screen, &dest);
}


first of all.. i get a compiler error from the line
if(event.type == SDL_Quit)
the error is: main.cpp: In function `int SDL_main(int, char**)': main.cpp:27: error: ISO C++ forbids comparison between pointer and integer make.exe: *** [main.o] Error 1 and i dont know what to do with it!? (using dev-c++ 4.9.9.2 with MingW and SDL) 2nd question, i commented out the if statement, to get it running anyways, and the window just shows and goes away again, how do i keep it on screen? thanks -niux edit: fixed source cmds

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
try to use 'SDL_QUIT' instead of 'SDL_Quit'

Share this post


Link to post
Share on other sites
yup SDL_QUIT compiled.. thank you!

but what about keeping the program running?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
modified version compiles and runs fine on my pc.
dont forget to put theImage.bmp file in one directory with the compiled version of your programm.
also, look for stderr.txt file - it contains runtime errors

Share this post


Link to post
Share on other sites
This is the main game loop I use in my SDL projects, it will loop until you press escape or issue a close message (click the "x"/alt+f4 etc):


bool QuitGame = false;
SDL_Event Event;

while(!QuitGame)
{
while(SDL_PollEvent(&Event))
{
//check for quit messages
if(Event.type == SDL_QUIT)
QuitGame = true;

if(Event.key.keysym.sym == SDLK_ESCAPE)
QuitGame = true;
}

//render frame
}


SDL_PollEvent will return 0 when there are no more events which will then let you render the next frame.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!