Original Post
Hi, so far my 2d game is coming along way better then I ever thought it would. I run in to small problems, but usially overcome them less then an hour later. Now it seems ive hit a larger problem. My theory of collision detection on a tile based map is if you move one pixel at a time, if the tile one pixel at any side of you isnt walkable then you dont allow movement. Now here is the code for my movement: now my problem begins. First, I only have two tiles, a wall and grass. How do I determine if something is or isnt walkable? Here is full code:
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE)
isRunning = false;
}
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] )
{
if (ypos -1 == notWalkable)
ypos -= 0;
else
ypos -= 1;
}
if ( keys[SDLK_DOWN] )
{
if (ypos +1 == notWalkable)
ypos += 0;
else
ypos += 1;
}
if ( keys[SDLK_LEFT] )
{
if (xpos -1 == notWalkable)
xpos -= 0;
else
xpos -= 1;
}
if ( keys[SDLK_RIGHT] )
{
if (xpos +1 == notWalkable)
xpos += 0;
else
xpos += 1;
}
//Mark St. Jean
//Brandon Walton
//WASTEDink 2004
//First online game alpha 1.0
#include <iostream>
#include <stdlib.h>
#include <winsock2.h>
#include <SDL/SDL.h>
//Functions
void host();
void join();
void drawMap();
void drawScene();
//draws bitmaps, x = o is top, y = 0 is left, sprite is the image
void draw(int x, int y, SDL_Surface *sprite, SDL_Surface *screen);
//Globals
//Main screen
SDL_Surface *titleScreen;
SDL_Surface *gameScreen;
//Tile Surfaces
SDL_Surface *tile1 = SDL_LoadBMP("gfx/tiles/tile1.bmp");
SDL_Surface *tile2 = SDL_LoadBMP("gfx/tiles/tile2.bmp");
//Sprites
SDL_Surface *sprite1 = SDL_LoadBMP("gfx/sprites/sprite1.bmp");
//Sprite coordniates
int xpos = 40;
int ypos = 320;
using namespace std;
int main(int argc, char *argv[])
{
Uint8* keys;
//Surfaces used in SDL
SDL_Surface *title1;
SDL_Surface *title2;
SDL_Surface *title3;
//Initalize Graphics
title1 = SDL_LoadBMP("gfx/title1.bmp");
title2 = SDL_LoadBMP("gfx/title2.bmp");
title3 = SDL_LoadBMP("gfx/title3.bmp");
//Sets transparency
SDL_SetColorKey(sprite1, SDL_SRCCOLORKEY, SDL_MapRGB(sprite1->format, 255, 100, 255));
//boolean used to determine if the game is running
bool isRunning;
//A SDL event, used to keep gamerunning.
SDL_Event event;
//unload function
atexit(SDL_Quit);
//sets the video mode that SDL uses
// Peremiters are window width, window height,color depth, then flags
titleScreen = SDL_SetVideoMode(760, 400, 0, SDL_HWSURFACE|SDL_DOUBLEBUF);
//gives the boolean a value
isRunning = true;
drawMap();
//Main game loop
while(isRunning == true)
{
//Poll event
SDL_PollEvent(&event);
//event that ends SDL and main game loop
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE)
isRunning = false;
}
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] )
{
if (ypos -1 == notWalkable)
ypos -= 0;
else
ypos -= 1;
}
if ( keys[SDLK_DOWN] )
{
if (ypos +1 == notWalkable)
ypos += 0;
else
ypos += 1;
}
if ( keys[SDLK_LEFT] )
{
if (xpos -1 == notWalkable)
xpos -= 0;
else
xpos -= 1;
}
if ( keys[SDLK_RIGHT] )
{
if (xpos +1 == notWalkable)
xpos += 0;
else
xpos += 1;
}
drawScene();
}
cout << "WELCOME 3 DA GAME!!! Well its actuially not here yet..." << endl;
cout << "This just tests the connection :)" << endl;
if (event.type == SDL_QUIT)
{
SDL_FreeSurface(title1);
SDL_FreeSurface(title2);
SDL_FreeSurface(title3);
isRunning = false;
}
return 0;
}
//SDL code---------------------------------------------
//Draws stuff to the screen
void draw(int x, int y, SDL_Surface *sprite, SDL_Surface *screen)
{
SDL_Rect source;
SDL_Rect dest;
source.x = 0;
source.y = 0;
source.w = sprite->w;
source.h = sprite->h;
dest.x = x;
dest.y = y;
dest.w = source.w;
dest.h = source.h;
SDL_BlitSurface(sprite, &source, screen, &dest);
return;
}
//DRAWS MAP!!!
void drawMap()
{
int map [10][19]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
for(int y = 0;y < 10; y++)
{
for(int x = 0;x < 19; x++)
{
if(map[y][x] == 1)
{
draw(x*40,y*40,tile1,titleScreen);
}
else if (map[y][x] == 0)
{
draw(x*40,y*40,tile2,titleScreen);
}
}
}
}
//draws background
void drawScene()
{
drawMap();
draw(xpos, ypos, sprite1, titleScreen);
SDL_Flip(titleScreen);
}