Thread: What is wrong with this C++ SDL code?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    9

    What is wrong with this C++ SDL code?

    I'm new to programming and I've spent some time learning the basics of c++ and the basics of SDL. I've started to work on a little project of a 2d game engine. My goal is to make a 24x16 tiles room and adding a controllable animated character for the player to move around with.

    I started writing the code for genereting the level itself. When I compile and run it I get no errors, but the screen stays black, without showing any of the tiles. Can somebody tell me what is wrong?

    Code:
    #include "SDL.h"
    #include "SDL_image.h"
    
    
    const int SCREEN_WIDTH = 384;
    const int SCREEN_HEIGHT = 256;
    const int SCREEN_BPP = 16;
    
    
    const int TILE_WIDTH = 16;
    const int TILE_HEIGHT = 16;
    const int TILES_NUMBER = 384;
    const int TILE_SPRITES = 4;
    const int TILE_WALL = 0;
    const int TILE_EDGE = 1;
    const int TILE_ROCKS = 2;
    const int TILE_PLATFORM = 3;
    
    
    SDL_Rect clipTiles[4];
    
    int main( int argc, char* args[] )
    {
        SDL_Init( SDL_INIT_EVERYTHING );
        screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
        tiles = IMG_Load( "tiles.png" );
    
    
        int x = NULL;
        int map[ 384 ] = {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
                          0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
                          0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
                          0, 2, 2, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 2, 2, 0,
                          0, 2, 2, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 2, 2, 0,
                          0, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 0,
                          0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
                          0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
                          0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
                          0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
                          0, 2, 2, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 2, 2, 0,
                          0, 2, 2, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 2, 2, 0,
                          0, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 0,
                          0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
                          0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
                          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        x = 0;
        for( int x = 0; x > 4; x++);
        {
            clipTiles[ x ].x = x * 16;
            clipTiles[ x ].y = 0;
            clipTiles[ x ].h = 16;
            clipTiles[ x ].w = 16;
        }
        x = 0;
        for( x = 0; x > 384; x++)
        {
            SDL_Rect offset;
            offset.x = (x)*16;
            offset.y = x/16;
            SDL_BlitSurface( tiles, &clipTiles[ map[ x ] ], screen, &offset );
        }
    
    
        SDL_Flip( screen );
    
    
        SDL_Delay (2000);
    
    
        return 0;
    }

    This is what I've done so far. I added the delay just to be able to see if the program was working. As you can see the character is not yet implemented into the game, and some of the variables are not yet being used.

    The tiles.png is a simple sprite sheet with 4 16x16 tiles drawn in a horisontal line after each other.

    EDIT: I just noticed that I set the variable x = 0 a little too many times. Sorry for that, but as far as I'm concerned it should have no effect on the program itself.
    Last edited by Prevenge; 10-08-2011 at 10:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is wrong with my code?
    By yann in forum C Programming
    Replies: 21
    Last Post: 10-10-2009, 07:51 AM
  2. wad wrong with this code?
    By tantan23434 in forum C Programming
    Replies: 3
    Last Post: 10-06-2009, 11:34 PM
  3. What's wrong with this code?
    By maxvcore in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2006, 10:55 PM
  4. What's wrong with my code (HELP)
    By n00by in forum C Programming
    Replies: 20
    Last Post: 08-11-2004, 03:15 PM
  5. What is wrong with my code?
    By kewee7197 in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2002, 06:40 AM