:blink: gpMain.c:
gpMain.g:
How would i add colision detection? even thought the camara stops at that certain pooint how would i keep the rectangle from moving past a certian cord?
Code:
#include <stdlib.h>
#include "gpdef.h"
#include "gpstdlib.h"
#include "gpgraphic.h"
#include "gpmain.h"
#include "gpstdio.h"
#include "gpfont.h"
#include "textdraw.h"
/* Defines */
#define TILESIZE 32
#define MAPH 10
#define MAPW 10
#define LCD_WIDTH 320
#define LCD_HEIGHT 240
/* Defines */
/* Misc */
char out_string[1024];
int py, px, ex, ey, exmax, eymax;
unsigned char map[MAPW][MAPH];
/* Misc */
void GpMain(void *arg)
{
Init();
GameEngine();
txtDraw();
}
void TimeDelay(int msec)
{
unsigned int n_tick = GpTickCountGet();
while ((GpTickCountGet() - n_tick) < (unsigned int)msec);
}
void Init()
{
int i, j;
GpClockSpeedChange(132000000, 0x24001, 2); /* speed = 133 Mhz */
/* Page-flipper - we need to keep track of which video page we're on. We'll start with page 1. */
nflip = 1;
/* Enable and clear LCD screen:
go through each page (this loop is a little pointless) and register it as a 'page' with the GP32. */
for(i = 0; i < 2; i++)
{
GpLcdSurfaceGet(&gpDraw[i], i);
}
/* Here we wipe the first screen clear white - it starts off with random garbage. We don't need to clear screen 0, since it
is a direct copy of screen 1. */
GpRectFill(NULL, &gpDraw[nflip], 0, 0, gpDraw[nflip].buf_w, gpDraw[nflip].buf_h, 0xff);
/* The GP32 needs to know to show screen 0 on the LCD, while we show screen 1 */
GpSurfaceSet(&gpDraw[0]);
/* The random values all come from one number, this case 36547. The random numbers will be
the same every time this way, but we'll improve that later */
srand(36547);
py = px = 400;
exmax = MAPW * TILESIZE;
eymax = MAPH * TILESIZE;
for(i = 0; i < MAPW; i++)
{
for(j = 0; j < MAPH; j++)
{
map[i][j] = (unsigned char)(rand() % 255);
}
}
}
void GameEngine()
{
while(1)
{
HandleInput();
ex = px - 160;
ey = py - 120;
if (ex < 0) ex = 0;
if (ey < 0) ey = 0;
if (ex > exmax) ex = exmax;
if (ey > eymax) ey = eymax;
DrawTiles(ex, ey);
GpRectFill(NULL, &gpDraw[nflip], px - ex - 5, py - ey - 5, 10, 10, 255);
GpRectFill(NULL, &gpDraw[nflip], px - ex - 4, py - ey - 4, 8, 8, 0);
GpSurfaceFlip(&gpDraw[nflip++]);
nflip &= 0x01;
}
}
void txtDraw()
{
GpTextOut(NULL, &gpDraw[0], 225, 225, "Seth", 10);
while(1);
}
void DrawTiles (int x, int y)
{
int xtile, ytile, xpos, ypos, i, j;
/* Getting starting tile to draw at */
xtile = x / TILESIZE;
ytile = y / TILESIZE;
/* Get the pixel offset to draw at
By the way, 'xpos = x & 31' is the same as 'xpos = x % 32' but faster */
xpos = x & (TILESIZE - 1);
ypos = y & (TILESIZE - 1);
/* Now draw the tiles */
for(i = 0; i < (320/TILESIZE + 1); i++)
{
for(j = 0; j < (240/TILESIZE + 2); j++)
{
/* Get tile number to draw, draw it */
GpRectFill(NULL, &gpDraw[nflip], i * TILESIZE - xpos, j * TILESIZE - ypos, 32, 32, map[xtile + i][ytile + j]);
}
}
}
void HandleInput()
{
ExKey = GpKeyGet();
if (ExKey & GPC_VK_LEFT) px--;
if (ExKey & GPC_VK_RIGHT) px++;
if (ExKey & GPC_VK_UP) py--;
if (ExKey & GPC_VK_DOWN) py++;
}
gpMain.g:
Code:
#ifndef __gpmain_h__
#define __gpmain_h__
void GpMain(void * arg);
void Init(void);
void GameEngine(void);
void DrawTiles(int x, int y);
void HandleInput(void);
void txtDraw(void);
int nflip, ExKey;
GPDRAWSURFACE gpDraw[2]; /* buffers */
#endif /*__gpmain_h__*/
How would i add colision detection? even thought the camara stops at that certain pooint how would i keep the rectangle from moving past a certian cord?