Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1056292 Posts in 42893 Topics- by 34843 Members - Latest Member: Zeyes

October 22, 2014, 10:39:17 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Rendering a tile map?
Pages: [1]
Print
Author Topic: Rendering a tile map?  (Read 616 times)
indietom
Level 2
**

Tom


View Profile Email
« on: November 22, 2012, 11:58:43 AM »

Hi!
I'm trying to make a game with tiles and I've done a 2d array like this:
Code:
int map[15][20]={
{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,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,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,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},
{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,0,0,0,0,1,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,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},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0},
{0,0,0,0,0,3,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,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,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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}
};

Here's the const_ints
Code:
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;

And from serching through out the internetz I've gatherd that I'm suppoused to make a loop like this.
Code:
for(int i=0;i<15;i++)
             for(int j=0;j<20;j++)
             {
                // what goes here?
             }

Now my question is how do I render it?
I have an sdl_surface that has the tileset.

Thanks for any help  Coffee
Logged

Code::Blocks
This is my profile, there are many like it but this one is mine.
Christian Knudsen
Level 10
*****



View Profile WWW Email
« Reply #1 on: November 22, 2012, 12:06:13 PM »

http://www.sdltutorials.com/sdl-coordinates-and-blitting
Logged

Laserbrain Studios
Currently working on Hidden Asset (TIGSource DevLog)
indietom
Level 2
**

Tom


View Profile Email
« Reply #2 on: November 23, 2012, 07:46:21 AM »

Is any of this right?
Code:
for(int i=0;i<15;i++)
             for(int j=0;j<20;j++)
             {
                SDL_Rect offset;
                offset.x = (x)*32;
                offset.y = x/32;
                SDL_BlitSurface(tiles, &clipTiles[map[i][j]], screen, &offset);
             }
Logged

Code::Blocks
This is my profile, there are many like it but this one is mine.
Christian Knudsen
Level 10
*****



View Profile WWW Email
« Reply #3 on: November 23, 2012, 08:02:43 AM »

Where are you defining x?
Logged

Laserbrain Studios
Currently working on Hidden Asset (TIGSource DevLog)
indietom
Level 2
**

Tom


View Profile Email
« Reply #4 on: November 23, 2012, 08:15:16 AM »

Code:
int x = NULL;
Logged

Code::Blocks
This is my profile, there are many like it but this one is mine.
impulse9
Level 5
*****



View Profile WWW
« Reply #5 on: November 23, 2012, 08:39:53 AM »

NULL*32 = 0, that won't do you any good.

I'm guessing you want something like:

Code:
offset.x = j*32;
offset.y = i*32;
Logged

indietom
Level 2
**

Tom


View Profile Email
« Reply #6 on: November 23, 2012, 09:03:11 AM »

NULL*32 = 0, that won't do you any good.

I'm guessing you want something like:

Code:
offset.x = j*32;
offset.y = i*32;

Oh dear god thank you  Coffee
Logged

Code::Blocks
This is my profile, there are many like it but this one is mine.
impulse9
Level 5
*****



View Profile WWW
« Reply #7 on: November 23, 2012, 09:07:26 AM »

You're welcome. Wink
Logged

indietom
Level 2
**

Tom


View Profile Email
« Reply #8 on: November 23, 2012, 11:18:17 AM »


Why is there a big black empty space there?
Here's the code.
Code:
for( int x = 0; x < 4; x++);
         {
             clipTiles[ x ].x = x*32;
             clipTiles[ x ].y = 0;
             clipTiles[ x ].h = 32;
             clipTiles[ x ].w = 32;
         }
         for( x = 0; x < 15; x++)
             for( y = 0; y < 20; y++)
             {
                SDL_Rect offset;
                offset.x = x*32;
                offset.y = y*32;
                SDL_BlitSurface(tiles, &clipTiles[map[x][y]], screen, &offset);
             }

The array is still the same.
Logged

Code::Blocks
This is my profile, there are many like it but this one is mine.
moi
Level 10
*****


DILF SANTA


View Profile WWW
« Reply #9 on: November 23, 2012, 11:33:37 AM »

is this not because your array is too small to cover the screen?
Logged

subsystems   subsystems   subsystems
impulse9
Level 5
*****



View Profile WWW
« Reply #10 on: November 23, 2012, 11:52:57 AM »

You should reverse the loop vars:

Code:
for( y = 0; y < 15; y++)
    for( x = 0; x < 20; x++)
Logged

indietom
Level 2
**

Tom


View Profile Email
« Reply #11 on: November 23, 2012, 12:09:10 PM »

You should reverse the loop vars:

Code:
for( y = 0; y < 15; y++)
    for( x = 0; x < 20; x++)

Tried using that but it crashed.
Logged

Code::Blocks
This is my profile, there are many like it but this one is mine.
Eigen
Level 10
*****


Jebus backups.


View Profile WWW
« Reply #12 on: November 23, 2012, 12:16:22 PM »

I'm pretty sure you're accessing your array out of bounds ...
Logged

indietom
Level 2
**

Tom


View Profile Email
« Reply #13 on: November 23, 2012, 01:18:12 PM »

I'm pretty sure you're accessing your array out of bounds ...

Oh, thanks. I fixed it.
Logged

Code::Blocks
This is my profile, there are many like it but this one is mine.
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic