Quick SDL 2d Example in 100Lines(C++)

Started by
5 comments, last by Roots 19 years, 7 months ago
hey folks I'm just learning SDL and After scouring many resources and documentation, Here is an example answering some of the common questions i've found. Enjoy, comment freely

/*
  Name: MySDL Game
  Copyright: OpenSource Of Course Of Course
  Author: Paul the Panopticon
  Date: 21/09/04 21:06
  Description: This app is simply an example to explore the   functionality of the
               SDL Librarly
*/
#include "SDL/SDL.h"   
#include <stdio.h>

//Prototype
int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);

//Create SDL Surfaces
SDL_Surface *back;
SDL_Surface *image;
SDL_Surface *screen;

//Movement Values
int xrate=0,yrate=0;//speed of sprite
int xpos =0,ypos =0;//position of sprite

//Accepts a pointer to surface, and coordinates
//It will accept a pointer to the sprite, were its supposed
//to go, and then blit it on the screen surface.
void DrawIMG(SDL_Surface *img, int x, int y){
  SDL_Rect dest;
  dest.x = x;
  dest.y = y;
  SDL_BlitSurface(img, NULL, screen, &dest);
}


int main(int argc,char* argv[]){
    Uint8* keys;
    
    //SDL must be initilaized before anything else
    if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0 ){
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
        return(1);
    }
    //This will set the video mode for the screen surface
    screen = SDL_SetVideoMode(800, 600,32,SDL_SWSURFACE|SDL_ANYFORMAT|SDL_FULLSCREEN);
    if ( screen == NULL ){
        fprintf(stderr, "Couldn't set 800x600x32 video mode: %s\n", SDL_GetError());
        return(1);
    }
    
    //Load the images
    back = SDL_LoadBMP("bg.bmp");
    image= SDL_LoadBMP("image.bmp");
    //Draw the background once..not really needed i guess.
    DrawIMG(back, 0, 0);

    //Start the game loop
    int done=0;
    while(done == 0){
        
        SDL_Event event;
        //Get user inputs and process them
        while (SDL_PollEvent(&event)){
            switch (event.type){
                case SDL_QUIT:
                    done = 1 ;
                    break;
                case SDL_KEYDOWN:
                     //evaluates wich key is down and
                     //sets sprite's speed accordingly
                     switch (event.key.keysym.sym){
                        case SDLK_ESCAPE:
                            done = 10;
                            break;
                        case SDLK_UP:
                            yrate = -10;
                            break;
                        case SDLK_DOWN:
                            yrate = 10; 
                            break;
                        case SDLK_LEFT:
                            xrate = -10;
                            break;
                        case SDLK_RIGHT:
                            xrate = 10;
                            break; 
                    }//end switch
                    break;
                case SDL_KEYUP:
                    switch (event.key.keysym.sym){
                        case SDLK_UP:
                            if(yrate < 0){yrate=0;}
                           break;
                        case SDLK_DOWN:
                            if(yrate > 0){yrate = 0; }
                            break;
                        case SDLK_LEFT:
                            if(xrate < 0){xrate =0;}
                            break;
                        case SDLK_RIGHT:
                            if (xrate > 0){xrate = 0;}
                            break; 
                    }//end switch
                    break;
            }//end switch
         }//End While
         //Update sprite position
         xpos=xpos+xrate;
         ypos=ypos+yrate;
         //draw the scene
         DrawIMG(back, 0, 0);
         DrawIMG(image, xpos, ypos);
         SDL_Flip(screen);
    }
    SDL_Quit();
    return(0);
}


EDIT: Just for some more clearification. I find documentation on SDL spotty at best. If you read my post in the beginner forum about my VB6 game you'll know im making a 2d tile-based rpg. Wel...I became frustrated with out objects, and decided I might as well just dive into c++ gaming. SDL seemed like a good library so I'm giving it a try. [Edited by - Panopticon on September 22, 2004 12:28:45 AM]
Advertisement
Where are the comments? If I'm a complete newb to SDL I'm going to be saying "what does this line do?" "what does that line do?". Just writing some code isn't enough. You need to *explain* the code.

Oh and by the way, I'm also using C++ and SDL to write a tile/sprite based 2D RPG game. :D If you want to you can check-out my sourceforge code and see basic sprite and map movement in action, but the code is for Linux and since you were using VB I think its likely that you are running Windoze... :P

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

Quote:Original post by Roots
Where are the comments? If I'm a complete newb to SDL I'm going to be saying "what does this line do?" "what does that line do?". Just writing some code isn't enough. You need to *explain* the code.

Oh and by the way, I'm also using C++ and SDL to write a tile/sprite based 2D RPG game. :D If you want to you can check-out my sourceforge code and see basic sprite and map movement in action, but the code is for Linux and since you were using VB I think its likely that you are running Windoze... :P


Explain things? Pishaw! Hogwash and poppy-cock! ;-P

yah, I guess I forgot about commenting. Its just a quick n' dirty example.

sigh...I guess is should go comment it....or it'll bug me
Quote:Original post by RootsIf you want to you can check-out my sourceforge code and see basic sprite and map movement in action, but the code is for Linux and since you were using VB I think its likely that you are running Windoze... :P


If you are using SDL correctly then the game should work on both Linux and Windows, correct?
Now I get a cookie!
Quote:Original post by Axenation
Quote:Original post by RootsIf you want to you can check-out my sourceforge code and see basic sprite and map movement in action, but the code is for Linux and since you were using VB I think its likely that you are running Windoze... :P


If you are using SDL correctly then the game should work on both Linux and Windows, correct?


Correct, but he said that he was designing this game in VB6 and, to my knowledge, there is no compiler/editor for VB6 code in Linux.

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

Quote:Original post by Roots
Correct, but he said that he was designing this game in VB6 and, to my knowledge, there is no compiler/editor for VB6 code in Linux.


Yes, but what does that have to do with anything? He said he was doing it in VB, he is doing it in C++ now...(and, btw, the SDL code he put here should compile and run ok on many systems, including linux and windows.
Quote:Original post by Roboguy
Quote:Original post by Roots
Correct, but he said that he was designing this game in VB6 and, to my knowledge, there is no compiler/editor for VB6 code in Linux.


Yes, but what does that have to do with anything? He said he was doing it in VB, he is doing it in C++ now...(and, btw, the SDL code he put here should compile and run ok on many systems, including linux and windows.


:) The reason why I said that was because I told him I had code that I could share demonstrating the basics of what he wants to do, but its written to be compiled on Linux and I don't have an .exe so he couldn't see it in action (which is always the best part) unless he was ambitious enough to compile it himself. My apologies for the confusion.

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

This topic is closed to new replies.

Advertisement