Original Post
okay... ill give the code first: desert.map looks like this: 1111111111111111111111111111 1000000000000000000000000001 1000000000000000000000000001 1000000000000000000000000001 1000000000222222000000000001 1000000002222222220000000001 1000000022222222222000000001 1000000000222222220000000001 1000000000002222200000000001 1000000000000000000000000001 1000000000000000000000000001 1111111111111111111111111111 where 0 = water, 1=grass, 2=desert. Im running it in 640x480, and tiles are 32px square... so there are 20 rows and 15 columns... The problem is: that when it takes the first 20 1's it prints like this: GGGGGGGGGGGGGGGGGGGG (Where G = grass) and then starts with the remaining 1's on the second row but still on the first row in the file: GGGGGGGGGGGGGGGGGGGG Okay now i need to fix this, so i guess i need to have an array: curr_screen[20][15]; or something like that but i need some help... thanks
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
SDL_Surface *screen;
int game_state;
int i;
int x;
int y;
int bytecount;
SDL_Surface *tiles[3];
int world[100*100];//map is 100x100
//this is to be called from your game loop
//this is the same code from the earlier example
int player_x;//player's x position in the world
int player_y;//player's y position in the wolrd
//...insert code that get player input and updates player_x and player_y based on player input.
//assume player character is at center of screen
//find start position to draw from in tile array
int start_x;//assume tiles are 32x32 and screen width is 640.
int start_y;//assume tiles are 32x32 and screen height is 480.
void init_tile()
{
/*
tiles[0] = SDL_LoadBMP("gfx/water.bmp");
tiles[1] = SDL_LoadBMP("gfx/desert.bmp");
tiles[2] = SDL_LoadBMP("gfx/grass.bmp");
*/
long size;
int bytecount;
int count;
FILE *f;
f=fopen("maps/desert.map","r");
if (f==NULL) perror ("Error opening file desert.map");
else
{
fseek (f, 0, SEEK_END);
size=ftell (f);
bytecount = size;
fseek (f, 0, SEEK_SET);
}
int player_index = 0;//stores our position in the world array
while(i<bytecount)
{
int temp = fgetc(f)-48;//read a tile index from the file
world[player_index] = temp;//store value from file in our world map array
//printf("%i", temp);
printf(", %i", world[player_index]);
++player_index;//move to next position in array
i=i+1;
}
game_state=2;
}
void draw()
{
int x, y;
int tile_index;
SDL_Surface *curr_tile = tiles[tile_index];
int draw_x = 0;//x position on screen to draw to
int draw_y = 0;//y position on screen to draw to
char *tilefile;//Tile to print
start_x= player_x-10;
start_y = player_y-7;
/*
SHORT DESCRIPTION: start_y/x = start position to draw from
draw_x/y = Position on screen to draw to
player_x/y = playersXpos
*/
for(y = start_y; y < start_y+15; ++y)//again, assumes 15 rows
{
for(x = start_x; x < start_x+20; ++x)//assumes 20 columns
{
tile_index = world[y*20+x]; //wich tile do we want to print? results in 0,1,2.
//printf("world = %i", world[y*20+x]);//debugging
//Printing the tiles got from tile_index
if(tile_index==0)
{
tilefile="gfx/water.bmp";
}
if(tile_index==1)
{
tilefile="gfx/grass.bmp";
}
if(tile_index==2)
{
tilefile="gfx/desert.bmp";
}
if(tile_index!=-38)
{
ShowBMP(tilefile, screen, draw_x, draw_y);
}
draw_x+=32;//move over 32 pixels because that is the width of one tile
}
draw_x=0;//When we have painted one row, reset X and put 32 to Y to start on the next row.
draw_y+=32;
}
}
void game_keypress()
{
SDL_EnableUNICODE(1);
SDL_Event event;
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
/* Look for a keypress */
case SDL_KEYDOWN:
switch( event.key.keysym.sym )
{
case SDLK_RIGHT:
player_x=player_x+32;
break;
case SDLK_LEFT:
player_x=player_x-32;
break;
case SDLK_ESCAPE:
fprintf(stderr, "\nQuiting game, have a nice day...\n");
game_state = 4;
break;
default:
break;
}
}
}
}