GP32 Text and Tiles


seth

Member
Joined
Apr 28, 2003
Messages
171
:blink: gpMain.c:
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?
 
k, i tried your code and this is what i did.

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"*/  /* because i dont have this file, this part is disabled */



/* 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(); 
}


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);

	txtDraw(); /* Move it here */

 GpSurfaceFlip(&gpDraw[nflip++]);
 nflip &= 0x01;
 

 }
}


void txtDraw()

{
   
GpTextOut(NULL, &gpDraw[nflip], 225, 225, "Seth", 10); /*set drawing layer to nflip*/

/* remove loop */

}

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++;

}

there are no changes to the gpmain.h file
 
One more thing

how would i collision detection(just simply like it cant go past the screen edge)

this line
Code:
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);
controlls the movment(and the handle movment line)
 
Whew! i did it.
k first i added two more global variables to the gpmain.c

Code:
int py, px, ex, ey, exmax, eymax, edgex = 160 , edgey = 120;

Then, in the GameEngine() procedure...
Code:
void GameEngine()
{

 while(1)
 {

 
 HandleInput();

 ex = px - edgex;
 ey = py - edgey;

 if (ex < 0) ex = 0;
 if (px <= 0) px = 0;
 if (ey < 0) ey = 0;
 if (py <= 0) py = 0;

 if (ex > exmax) ex = exmax;
 if (px >= exmax + edgex * 2) px = exmax + edgex * 2;
 if (ey > eymax) ey = eymax; 
 if (py >= eymax + edgey * 2) py = eymax + edgey * 2;

 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);

	txtDraw();

 GpSurfaceFlip(&gpDraw[nflip++]);
 nflip &= 0x01;
 

 }
}

This is what i what to do also so im glad someone is thinking the same way :lol:
 
thx but, is there anyway i can move the edges back a little? i cant get the vars in the int to work right
 
Im not sure exactly what u mean but i assume u want the box to stop a bit further from the edge of the screen and towards the center of the screen.

Sorry about the ints :D

Explaination of the border variable:
If higher than 0, the black box will stop further away from the screen's edge but will still be on the screen.
If lower than 0, the black box will start disappearing past the screen's edge and off the screen.

Code:
void GameEngine()
{
	int border = 10; /* makes an invisible border */

 while(1)
 {

 
 HandleInput();

 ex = px - edgex;
 ey = py - edgey;

 if (ex < 0) ex = 0;
 if (px <= border) px = border;
 if (ey < 0) ey = 0;
 if (py <= border) py = border;

 if (ex > exmax) ex = exmax;
 if (px >= exmax + (edgex * 2) - border) px = exmax + edgex * 2 - border;
 if (ey > eymax) ey = eymax; 
 if (py >= eymax + (edgey * 2) - border) py = eymax + edgey * 2 - border;

 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);

	txtDraw();

 GpSurfaceFlip(&gpDraw[nflip++]);
 nflip &= 0x01;
 

 }
}
 
it works for the right and bottom side of the screen but not the top and left

im assuming i need to add the border line to the them?
 
Back
Top