Skip to main content
GameDev.net gamedev.net
🔒 Locked

scrolling

Started by darkbard Mar 20, 2003 at 3:55 AM 0 replies 400+ views
Original Post
darkbard
darkbard
Anyone could help me in adding smooth scroll to the engine below??
  
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>

#define MAP_X			40
#define MAP_Y			40
#define SCREEN_X		800
#define SCREEN_Y		600
#define TILE_X			64
#define TILE_Y			32
#define SCROLL_SPEED	5

typedef struct{
	int tile;
}MAP_INFO;

MAP_INFO map[MAP_Y][MAP_X];
SDL_Surface *screen,*tiles;
int xOffset = 0, yOffset = 0;
int xAbsolute = 1, yAbsolute = 0;
int xMaxScroll = TILE_X*MAP_X;
int yMaxScroll = TILE_Y*MAP_Y;
int xMaxDraw = SCREEN_X/TILE_X+2;
int yMaxDraw = (SCREEN_Y/TILE_Y+2)*2;
int xMap = 0,yMap = 0;

void setupMap(void);
void loadDataFiles(void);
void mainLoop(void);
void drawScene(void);
void xScroll(int);
void yScroll(int);
//void scrollScreen(int,int);
SDL_Rect getTile(int,int);

int main(int argc, char *argv[]){
	setupMap();
	Uint8 *keys;

    screen = SDL_SetVideoMode(SCREEN_X,SCREEN_Y,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
 	if ( screen == NULL ){
  		printf("Unable to set %sx%s video: %s\n", SCREEN_X, SCREEN_Y, SDL_GetError());
    	exit(1);
    }
	if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ){
 		printf("Unable to init SDL: %s\n", SDL_GetError());
    	exit(1);
    }
    loadDataFiles();
    mainLoop();
    atexit(SDL_Quit);
}

void setupMap(void){
	int x,y;
	for(x = 0;x < MAP_X;x++){
		for(y = 0;y < MAP_Y;y++){
			map[y][x].tile = 1;
		}
	}
}

void loadDataFiles(void){
	tiles = SDL_LoadBMP("data/BaseTiles.bmp");
	SDL_SetColorKey(tiles,SDL_SRCCOLORKEY,SDL_MapRGB(tiles->format,252,2,252));
}

void mainLoop(void){
	int done = 0;
	Uint8 *keys;
	while(!done){
		SDL_Event event;
  		while ( SDL_PollEvent(&event) ){
    		if ( event.type == SDL_QUIT ) {
      			done = 1;
         	}
      		if ( event.type == SDL_KEYDOWN ){
        		if ( event.key.keysym.sym == SDLK_ESCAPE ) {
          			done = 1;
             	}
            }
        }
        keys = SDL_GetKeyState(NULL);
        if ( keys[SDLK_UP] ) {
        	yScroll(-SCROLL_SPEED);
        }
        if ( keys[SDLK_DOWN] ) {
        	yScroll(SCROLL_SPEED);
        }
        if ( keys[SDLK_LEFT] ) {
        	xScroll(SCROLL_SPEED);
        }
        if ( keys[SDLK_RIGHT] ) {
        	xScroll(-SCROLL_SPEED);
        }
		drawScene();
		SDL_Flip(screen);
	}
}

void drawScene(void){
	int x,y;
	int xUp = 0,yUp = -TILE_Y/2;
	SDL_Rect pos,port;
	for(y = yMap;y < yMaxDraw+yMap;y++){
		if(y%2 == 0){
			xUp = 0;
		}else{
			xUp = -(TILE_X/2);
		}
		for(x = xMap;x < xMaxDraw+xMap;x++){
			pos.x = xUp + xOffset;
			pos.y = yUp + yOffset;
			port = getTile(x,y);
			SDL_BlitSurface(tiles,&port,screen,&pos);
			xUp += TILE_X;
		}
		yUp += TILE_Y/2;
	}
}

SDL_Rect getTile(int x, int y){
	SDL_Rect rect;
	rect.x = map[y][x].tile * TILE_X;
	rect.y = 0;
	rect.w = TILE_X;
	rect.h = TILE_Y;
	return rect;
}

void xScroll(int value){
	return;
}

void yScroll(int value){
	return;
}
 
I got a lot of problems, probably because my engine is not developed very well...if so, anyone could help me to make it better?? tnx p.s: I use SDL lib, but I think you will have no problems to read it also if you use directx or other libs. [edited by - darkbard on March 20, 2003 4:59:30 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.