# Realy hard problem... scrolling game..

## Recommended Posts

Okay im trying to make a scrolling game engine. Heres code:
[SOURCE]
#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

void init_tile()
{

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);
//fclose (f);
bytecount = size;
}

int world_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[world_index] = temp;//store value from file in our world map array

++world_index;//move to next position in array
i=i+1;
}
game_state = 2;
}

//this is to be called from your game loop
//this is the same code from the earlier example
int world_x;//player's x position in the world
int world_y;//player's y position in the wolrd

//...insert code that get player input and updates world_x and world_y based on player input.

void draw()
{
int x, y;

int screen_x = 0;//x position on screen to draw to
int screen_y = 0;//y position on screen to draw to

//assume player character is at center of screen
//find start position to draw from in tile array
int start_x = world_x-10;//assume tiles are 32x32 and screen width is 640.
int start_y = world_y-7;//assume tiles are 32x32 and screen height is 480.

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
{
//find out which tile we are currently looking for
int tile_index = world[y*20+x];

printf("\ntile_index=%i", tile_index);

//use that value to get the tile from the tile set array
//SDL_Surface *curr_tile = tiles[tile_index];

/*
//draw the current tile.  sorry, dont know real funtion name
ShowBMP(curr_tile, screen, start_x, start_y);
*/

if(tile_index==0)
{
ShowBMP("gfx/water.bmp", screen, start_x, start_y);
}

screen_x+=32;//move over 32 pixels because that is the width of one tile

}

//we have now finished drawing a row
screen_x=0;//reset screen x position to 0
screen_y+=32;//increment the screen y counter by 32 (height of one tile) so that we draw to the next row the next time through the loop
}
SDL_Delay(1000);
}

[/SOURCE]
[/source] it only prints out the first tile, water. The desert.map looks something like this: WDWDDWWWDWDGGDWWD WWDWDGGDWWDGGDWWD WWDWDGGDWWDGGDWWD WWDWDGGDWWDGGDWWD WWDWDGGDWWDGGDWWD W=water, D=desert, G=Grass.. okay you probably noticed that i added printf("\ntile_index=%i", tile_index); to debug... tile_index is supposed to hold 0,1 or 2, for water, grass, desert. i get this output: tile_index=-49 tile_index=-49 tile_index=-49 tile_index=-49 tile_index=-49 tile_index=-49 tile_index=-49 tile_index=-49 tile_index=-49 and so on for ever and ever... thanks for helping. [Edited by - peb on November 10, 2004 11:43:12 AM]

##### Share on other sites
use the source tags to make it easier to read, and if im not mistaken theres no main function there?

##### Share on other sites
Quote:
 Original post by kzarif im not mistaken theres no main function there?

the thing is that that is one of many files i have in a make file, but thats the file that the drawing and making up the world is in.

##### Share on other sites
It looks to me (from my quick scan of your code) that when you move to position to the end of the file to get the byte count, you never move the position back to the beginning.

You then keep trying to read from the end of the file, and fgetc() is returning EOF. I'm willing to bet that EOF is defined as -1, so fgetc(f)-48 is always giving you -49.

A better way to go about it would be to start at the beginning of the file and then keep reading until fgetc() returns EOF.

char c;c = fgetc(f) - 48;while(c != EOF){  //Do something with c  //Get the next character  c  = fgetc(f) - 48;}

You could also use a for loop.

##### Share on other sites
woops sorry

Quote:
Original post by peb
Quote:
 Original post by kzarif im not mistaken theres no main function there?

the thing is that that is one of many files i have in a make file, but thats the file that the drawing and making up the world is in.

no problem :)

##### Share on other sites
hmm i made this program about 3 months ago... cant exactly understand what ive done lol :) Why do i have -48? im pretty lost here lol

##### Share on other sites
Quote:
 Original post by pebhmm i made this program about 3 months ago... cant exactly understand what ive done lol :) Why do i have -48? im pretty lost here lol

It might be that you are trying to convert from the ASCII representation of a number to the number itself. The ASCII code for 0 is 48, for 1 49, and so on, therefore subtracting 48 nets you the number itself.

##### Share on other sites
Nah, he already subtracts 48, hence the minus sign - he is reading a 0 for every value.

This is because the code opens the file, seeks to the end of it, and then starts reading from the end onwards . . . [wink]

[edit: erm, like monkey cookie said, infact.]

##### Share on other sites
Quote:
Original post by TheWanderer
Quote:
 Original post by pebhmm i made this program about 3 months ago... cant exactly understand what ive done lol :) Why do i have -48? im pretty lost here lol

It might be that you are trying to convert from the ASCII representation of a number to the number itself. The ASCII code for 0 is 48, for 1 49, and so on, therefore subtracting 48 nets you the number itself.

Ohh yeah thats right thanx....

okay now i tried to make a backbuffering function... i made it like this:

void ShowBMP(char *file, SDL_Surface *screen, int x, int y){	SDL_Surface *image;	SDL_Rect dest;	SDL_Surface *bbuf;    /* Load the BMP file into a surface */	    image = SDL_LoadBMP(file);    if ( image == NULL ) {        fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());        return;    }    /* Blit onto the screen surface.       The surfaces should not be locked at this point.     */	dest.x = x;	dest.y = y;	dest.w = image->w;	dest.h = image->h;	SDL_BlitSurface(image, NULL, bbuf, &dest);	SDL_BlitSurface(bbuf, NULL, screen, &dest);    /* Update the changed portion of the screen */	SDL_UpdateRects(screen, 1, &dest);}

but it dosnt work... its all black

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628372
• Total Posts
2982305

• 10
• 9
• 13
• 24
• 11