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

  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.

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    9
    I just noticed a small mistake.
    Line 58 should be offset.x = (x%24) * 16
    Line 59 should be offset.y = (x/24) * 16

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    There's actually quite a bit going on. Here's my synopsis:

    1)SDL_Image needs to be initialised just as SDL does, with a call to IMG_Init.

    2)Keep in mind that in a for loop, for(a;b;c) loops until b is false, or likewise, as long as b is true.

    2.5)
    Code:
        for( int x = 0; x > 4; x++);
        {
            clipTiles[ x ].x = x * 16;
            clipTiles[ x ].y = 0;
            clipTiles[ x ].h = 16;
            clipTiles[ x ].w = 16;
        }
    Stray semicolon here; also, you declared another variable 'x' local to the for loop. I don't know if it was intentional and it doesn't affect anything in this case but I'd suggest you try to be as consistent as possible w/regard to this to avoid confusion.

    3)
    Code:
            offset.x = (x)*16;
            offset.y = x/16;
    My guess is that since you have parentheses around x, you meant to do more here and never got back to it (hint hint ) You probably would have got here eventually once the rest of the program was fixed. EDIT: Yep you got it.
    Consider this post signed

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I am not familiar with SDL but usually when you get a black screen it can be a number of things. I will list a few that might help:

    • Incorrect transformations IE: object is in the wrong spot and is being drawn....just nowhere near where you think it is
    • Incorrect culling mode - your object is being culled due to the winding order of the triangles used to draw it
    • Lighting is enabled but there is no light in the world
    • Lighting is enabled but the object in question has no normals
    • Lighting is enabled but the transformation pipeline is not set to normalize normals which yields wacky results
    • Projection matrix is incorrect
    • Camera is in the wrong location - IE: the camera is too close to the object and therefore the object is being culled by the frustrum
    • Lighting is disabled but the vertices have no color in them
    • Lighting is disabled; vertices have color; but the API is set to take the diffuse color from the material instead of the vertices
    • Wrong or no material for a mesh
    • Incorrect lighting parameters for the light
    • Z buffer is enabled for 2D with an ortho matrix being set - z buffer should be set to off for 2D ortho projection matrix


    When I get a black screen (and I do far more often than one might think) the first thing I do is clear the backbuffer to some other color than black. Often times this will reveal that the object is indeed there but is completely black. I normally clear it to 0,0,1,1 or pure blue.

    Again I do not know SDL but maybe some of those statements might lead you in the right direction.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    9
    Thanks for the help.
    Turned out I did another horrible mistake when writing the for commands. I had written > instead of < which I guess means that it was just skipped since b was actually false from the start.

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