Help With Test Demo App
Posted: November 1st, 2012, 4:55 pm
Alright so I've been playing around with some code and I can't figure out why my sprite eship (enemy ship) will move right across the screen but will not change directions. Written in C for PSXSDK.
Any help appreciated!
Code: Select all
#include <stdio.h>
#include <psxgpu.h>
#include <psx.h>
GsDispEnv game_dispenv;
GsDrawEnv game_drawenv;
GsImage game_image;
GsSprite ship;
GsSprite shot;
GsSprite eship;
int refresh_needed=0;
int emove=true;
unsigned char file_buffer[0x30000];
unsigned int prim_list[4000];
int ship_coordinates[2];
int shot_coordinates[2];
int eship_coordinates[1];
void load_file_into_buffer(char *fname)
{
FILE *f;
int sz, fd;
f = fopen(fname, "rb");
fseek(f, 0, SEEK_END);
sz = ftell(f);
fseek(f, 0, SEEK_SET);
printf("%s (%d)\n", fname, sz);
fread(file_buffer, sizeof(char), sz, f);
fclose(f);
}
void InitializePSX (){
PSX_Init();
GsInit();
GsClearMem();
GsSetVideoMode(320,240, VMODE_NTSC);
game_dispenv.x=0;
game_dispenv.y=0;
GsSetDispEnv(&game_dispenv);
game_drawenv.x=0;
game_drawenv.y=0;
game_drawenv.w=320;
game_drawenv.h=240;
game_drawenv.draw_on_display=1;
game_drawenv.ignore_mask=0;
game_drawenv.dither=0;
game_drawenv.set_mask=0;
GsSetDrawEnv(&game_drawenv);
GsSetList(prim_list);
}
void ClearScreen(){
GsRectangle gs_rect;
gs_rect.x = 0;
gs_rect.y = 0;
gs_rect.w = 320;
gs_rect.h = 240;
gs_rect.r = 0;
gs_rect.g = 0;
gs_rect.b = 0;
gs_rect.attribute = 0;
GsSortRectangle(&gs_rect);
GsDrawList();
while(GsIsDrawing());
}
void DoubleBuffering(){
if(game_dispenv.y == 0)
{
game_dispenv.y = 256;
game_drawenv.y = 0;
}
else
{
game_dispenv.y = 0;
game_drawenv.y = 256;
}
GsSetDispEnv(&game_dispenv);
GsSetDrawEnv(&game_drawenv);
}
void game_vblank_handler(){
refresh_needed++;
}
void ShowFrameBuffer(int ShowIt){
if (ShowIt==0)
GsSetVideoMode(320,240, VMODE_NTSC);
if (ShowIt==1)
GsSetVideoMode(640,480, VMODE_NTSC);
}
void GamePadPlayerOneRead(){
unsigned short pad1, pad2;
PSX_ReadPad(&pad1, &pad2);
if (pad1 & PAD_LEFT){
if (ship_coordinates[0] > 6){
ship_coordinates[0]-=3;
}else ;
}
if (pad1 & PAD_RIGHT){
if (ship_coordinates[0] < 284){
ship_coordinates[0]+=3;
}else ;
}
if (pad1 & PAD_UP){
if (ship_coordinates[1] > 6){
ship_coordinates[1]-=3;
}else;
}
if (pad1 & PAD_DOWN){
if (ship_coordinates[1] < 194){
ship_coordinates[1]+=3;
}else;
}
if (pad1 & PAD_CROSS){
if (shot_coordinates[1] < 0){
shot_coordinates[0] = ship_coordinates[0]+11;
shot_coordinates[1] = ship_coordinates[1]-17;
}else;
}
}
void GameDrawAllSprites(){
ClearScreen();
while(GsIsDrawing());
ship.x=ship_coordinates[0];
ship.y=ship_coordinates[1];
shot.x=shot_coordinates[0];
shot.y=shot_coordinates[1]-=6;
if (emove=true){
eship.x=eship_coordinates[0]+=1;
}
if (emove=false){
eship.x=eship_coordinates[0]-=1;
}
if (eship.x > 208){
emove=false;
}else; emove=true;
if (shot_coordinates[1] <= -15){
shot_coordinates[1] = -50;
}else;
GsSortSprite(&ship);
GsSortSprite(&shot);
GsSortSprite(&eship);
GsDrawList();
while(GsIsDrawing());
DoubleBuffering();
}
int main(){
InitializePSX();
SetVBlankHandler(game_vblank_handler);
ship_coordinates[0]=145;
ship_coordinates[1]=155;
shot_coordinates[0]=-50;
shot_coordinates[1]=-50;
eship_coordinates[0]=0;
GsLoadFont(320, 240, 360, 240);
load_file_into_buffer("cdrom:ship.TIM;1");
GsImageFromTim(&game_image,file_buffer);
GsUploadImage(&game_image);
GsSpriteFromImage(&ship,&game_image,1);
ship.x=ship_coordinates[0];
ship.y=ship_coordinates[1];
ship.w=30;
ship.h=40;
GsSortSprite(&ship);
load_file_into_buffer("cdrom:shot.TIM;1");
GsImageFromTim(&game_image,file_buffer);
GsUploadImage(&game_image);
GsSpriteFromImage(&shot,&game_image,1);
shot.x=ship_coordinates[0]+=8;
shot.y=ship_coordinates[1]-=-15;
shot.w=7;
shot.h=17;
GsSortSprite(&shot);
load_file_into_buffer("cdrom:eship.TIM;1");
GsImageFromTim(&game_image,file_buffer);
GsUploadImage(&game_image);
GsSpriteFromImage(&eship,&game_image,1);
eship.x=eship_coordinates[0];
eship.y=0;
eship.w=25;
eship.h=35;
GsSortSprite(&eship);
GsDrawList();
while(GsIsDrawing());
while(1){
while (refresh_needed){
GamePadPlayerOneRead();
GameDrawAllSprites();
refresh_needed--;
}
}
}