Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Tiling with SDL
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Zren




PostPosted: Wed Apr 15, 2009 10:18 pm   Post subject: Tiling with SDL

I've been learning C++ and SDL from LazyFoo.net. After awhile I decided to attempt at making a platformer, since I learn best when trying to convert the teaching into a game. Unfortunatly I can't seem to understand how to tile surfaces...properly.

clip is the x,y,w,h of the source image that is being use to tile.

My logic is this:
Load Image.
Split into three.
Tile middle part.
Send X,Y, Width & Height of Clip to function.
Print to temporary Surface.
Print temp surface to screen.

I can't seem to understand why this won't work.

c++:

    //image is 100x20. image is using the pixels from 10 <= x <= 90 for the tiled part.
    part.x = 10;
    part.y = 0;
    part.w = box.w - 20;
    part.h = 20;
   
    //apply_surface( x, y, source, destination)
    apply_surface( box.x+10, box.y, tile_surface( part.w, part.h, imgPlatform, &part ), screen );

SDL_Surface *tile_surface( int width, int height, SDL_Surface* source, SDL_Rect* clip = NULL )
{
    SDL_Surface* outputImage = NULL;
    SDL_Rect offset;
    int tiledWidth =2, tiledHeight =2; // On purpose to just make a simple 2x2 tile for debugging.
    //tiledWidth = width / clip.w; //Constants to be changed to new parameters.
    //tiledHeight = height / clip.h;
   
    for ( int y = 0; y < tiledHeight; y++ )
    {
        for ( int x = 0; x < tiledWidth; x++ )
        {
            apply_surface( x*clip.w, y*clip.h, source, outputImage, clip);
        }
    }
   
    return outputImage;
}
Sponsor
Sponsor
Sponsor
sponsor
saltpro15




PostPosted: Fri Apr 17, 2009 5:24 pm   Post subject: RE:Tiling with SDL

should work...
what's your compile command?
CodeMonkey2000




PostPosted: Fri Apr 17, 2009 9:07 pm   Post subject: RE:Tiling with SDL

You do realize how small a 2x2 clip space is right? And outputImage will always equal null. Also I don't follow your logic in tile_surface. What are you trying to accomplish? Finally, the way lazyfoo structured his code is convenient for tutorials, and so it isn't very practical at all, you probably should reorganize it into something easier to reuse (make a library). With the current system you have to manually set up and pass the screen surface every time you start a new program.
Zren




PostPosted: Sun Apr 19, 2009 1:33 pm   Post subject: Re: RE:Tiling with SDL

CodeMonkey2000 @ Fri Apr 17, 2009 9:07 pm wrote:
You do realize how small a 2x2 clip space is right? And outputImage will always equal null. Also I don't follow your logic in tile_surface. What are you trying to accomplish? Finally, the way lazyfoo structured his code is convenient for tutorials, and so it isn't very practical at all, you probably should reorganize it into something easier to reuse (make a library). With the current system you have to manually set up and pass the screen surface every time you start a new program.


You do realize how small a 2x2 clip space is right?
I meant a 2x2 Tile set. so if one tile was 20x20px, then the output should be 40x40px. Large enought to notice.

And outputImage will always equal null.
How? It is initialized as NULL, but I'm adding to it through the for loops. Do I have to initialize a size of a SDL_Surface?

Library-smibrary. I need the practice of setting everything up. Maybe when I 'graduate' this tutorial I'll consider a library.
CodeMonkey2000




PostPosted: Sun Apr 19, 2009 1:45 pm   Post subject: RE:Tiling with SDL

You didn't allocate enough memory for the pixel data in outputImage. Didn't you get any run-time errors?

EDIT: You may want to use SDL_CreateRGBSurface
Zren




PostPosted: Sun Apr 19, 2009 3:16 pm   Post subject: Re: Tiling with SDL

Sick, I got it working. Thanks CodeMonkey. I had no run time errors though since I no error catching. I'm lazier than the foo himself. If there's something in the standard library then no I didn't. My compiler is Dev-C++. I'm also using the SDL libraries linked from lazyfoo's tutorials.

And saltpro, thanks for posting, but I'm not sure what you meen by that unless it's the linker properties, since I'm using and IDE. The program compiles fine, and wasn't calling anything that wasn't already included in other programs from the tutorials.
saltpro15




PostPosted: Sun Apr 19, 2009 4:16 pm   Post subject: RE:Tiling with SDL

well Zren, I know some people compile with say g++ whatev.cpp -o game -lSDL when really you want g++ whatev.cpp -o game -lSDL_image. using the first compile command from the terminal/prompt generates errors while the latter doesn't

and you didn't state you were using an IDE, so give me a break Wink
CodeMonkey2000




PostPosted: Sun Apr 19, 2009 5:17 pm   Post subject: Re: Tiling with SDL

Zren @ Sun Apr 19, 2009 3:16 pm wrote:
I had no run time errors though since I no error catching. I'm lazier than the foo himself. If there's something in the standard library then no I didn't. My compiler is Dev-C++. I'm also using the SDL libraries linked from lazyfoo's tutorials.


That doesn't matter, windows should have terminated your program.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: