#include <nds.h>
#include "fat_driver/gbamp_cf.c"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jpeg_lib/gba-jpeg.h"
#include "jpeg_lib/gba-jpeg-decode.c"
#include <nds/arm9/console.h>
#include "tiles.h"
#include "text.h"
#include "bat.h"
#define NUM_SPRITES 128
sSpriteEntry OAMCopy[128];
typedef struct
{
int x,y;
int dx, dy;
sSpriteEntry* oam;
int gfxID;
}Sprite;
void MoveSprite(Sprite* sp)
{
sp->oam->attribute[1] &= 0xFE00;
sp->oam->attribute[1] |= (sp->x & 0x01FF);
sp->oam->attribute[0] &= 0xFF00;
sp->oam->attribute[0] |= (sp->y & 0x00FF);
}
void initOAM(void)
{
int i;
for(i = 0; i < 128; i++)
{
OAMCopy[i].attribute[0] = ATTR0_DISABLED;
}
}
void updateOAM(void)
{
unsigned int i;
for(i = 0; i < 128 * sizeof(sSpriteEntry) / 4 ; i++)
{
((uint32*)OAM)[i] = ((uint32*)OAMCopy)[i];
}
}
void grab_a_graphic(char* filename,u16* buffer,u16 width,u16 height);
void drawgraphic(u16* buffer,u16* image,u16 width,u16 height,u16 x_pos,u16 y_pos);
void draw_map(u16);
void levelloader(u16 level_no);
void read_width_height(u16);
void draw_anim_image(u16* buffer,u16* image,u16 width,u16 height,u16 tilewidth,u16 tileheight,u16 frame_no,u16 obj_width);
void draw_text(char* dtext,u16 tst,u16 ist);
u16 instr(char* ,char* ,u16);
void readline(char* ,char** );
void readfile(char* ,char** );
char* mid(char* ,u16 ,u16 );
u16 len(char*);
void closefile(char*);
u16* vram;
typedef struct{
u16 height;
u16 width;
u16 tilewidth;
u16 tileheight;
u16 counter;
}LEVEL;
LEVEL levels[60];
u16 map[50][50];
u16 anim[50][50];
u16 background[256*192];
u16 obj_number;
Sprite sprites[NUM_SPRITES];
int main(){
powerON(POWER_ALL);
irqInit();
irqSet(IRQ_VBLANK,0);
WAIT_CR=0xe800;
vramSetMainBanks( VRAM_A_MAIN_SPRITE, //A and B maped consecutivly as sprite memory
VRAM_B_MAIN_SPRITE, //this gives us 256KB which is the max
VRAM_C_MAIN_BG_0x6000000, //map C to background memory
VRAM_D_LCD //not using D
);
//set the video mode
videoSetMode( MODE_0_2D |
DISPLAY_SPR_ACTIVE | //turn on sprites
DISPLAY_BG0_ACTIVE | //turn on background 0
DISPLAY_SPR_1D | //this is used when in tile mode
DISPLAY_SPR_1D_BMP //and this in bitmap mode
);
//videoSetModeSub(MODE_0_2D|DISPLAY_SPR_ACTIVE|DISPLAY_BG0_ACTIVE|DISPLAY_SPR_1D|DISPLAY_SPR_1D_BMP);
//vramSetMainBanks(VRAM_A_LCD, VRAM_B_LCD, VRAM_C_SUB_BG, VRAM_D_SUB_SPRITE);
//vram=VRAM_A;
initOAM();
for (int x=0; x<224*32 ; x++)
SPRITE_GFX[x]=tiles[x]|BIT(15);
levelloader(1);
draw_map(1);
//draw_text("NINO IS THE GREATEST!!",10,10);
while(1)
{
swiWaitForVBlank();
updateOAM();
}
return(0);
}
void grab_a_graphic(char* filename , u16* buffer , u16 width , u16 height)
{
FAT_InitFiles();
int handle = FAT_fopen(filename, "r");
int size=FAT_GetFileSize();
char* text = (char*) malloc (size+1);
while (!FAT_feof(handle))
{
FAT_fread((void*)text, size, 1, handle);
}
FAT_fclose(handle);
//now decompress image and drop it in an array
WAIT_CR &= ~0x80;
JPEG_DecompressImage((const unsigned char*)text , buffer , width , height);
WAIT_CR |= 0x80;
free(text);
}
void drawgraphic(u16* buffer,u16* image,u16 width,u16 height,u16 x_pos,u16 y_pos)
{
buffer += y_pos * SCREEN_WIDTH + x_pos;
for(int i = 0; i < height; ++i)
{
u16* line = buffer + (SCREEN_WIDTH * i);
u16* color= image + (width * i);
for(int j = 0; j < width; ++j)
{
*line++ = (*color++)|BIT(15);
}
}
}
void draw_anim_image(u16* buffer,u16* image,u16 width,u16 height,u16 tilewidth,u16 tileheight,u16 frame_no,u16 obj_width)
{
buffer += height * SCREEN_WIDTH + width;
image += tilewidth*frame_no;
for(int i = 0; i < tileheight; ++i)
{
u16* line = buffer + (SCREEN_WIDTH * i);
u16* color= image + (obj_width * i);
for(int j = 0; j < tilewidth; ++j)
{
*line++ = (*color++)|BIT(15);
}
}
}
void levelloader(u16 level_no)
{
read_width_height(level_no);
char* filein;
char fname[128];
u16 x=0;
u16 point_index_t_or_f=0;
sprintf(fname, "/BREAK_OUT_MANIA/levels/level%d.txt", level_no);
readfile(fname,&filein);
char* point_index;
char* maps;
readline(filein,&maps);
free(maps);
readline(filein,&maps);
free(maps);
for(int yx=0 ; yx<levels[level_no].height ; yx++)
{
readline(filein,&maps);
x=0;
for(int ix=0 ; ix<levels[level_no].width ; ix++)
{
point_index=mid(maps,x,instr(maps,",",x));
if (len(point_index)>1) point_index_t_or_f=1;
else point_index_t_or_f=0;
if (point_index_t_or_f)
{
map[ix][yx]=atol(mid(maps,x,1));
anim[ix][yx]=atol(mid(point_index,2,3));
x=x+3+1;
}
else
{
map[ix][yx]=0;
x=x+1+1;
}
}
free(maps);
}
closefile(filein);
levels[level_no].counter=0;
for (int yx=1 ; yx<levels[level_no].height ; yx++)
{
for (int ix=1 ; ix<levels[level_no].width ; ix++)
{
if (anim[ix][yx]>0) levels[level_no].counter=levels[level_no].counter+1;
}
}
}
void draw_map(u16 level_no)
{
obj_number=0;
for (int yx=0 ; yx<1/*levels[level_no].height*/ ; yx++)
{
for (int xx=0 ; xx<1/*levels[level_no].width*/;xx++)
{
if (anim[xx][yx]>0)
{
//random place and speed
sprites[obj_number].x = xx*levels[level_no].tileheight;
sprites[obj_number].y = yx*levels[level_no].tilewidth;
sprites[obj_number].oam = &OAMCopy[obj_number];
sprites[obj_number].gfxID = 0;//anim[xx][yx];
//set up our sprites OAM entry attributes
sprites[obj_number].oam->attribute[0] = ATTR0_BMP;
sprites[obj_number].oam->attribute[1] = ATTR1_SIZE_32;
sprites[obj_number].oam->attribute[2] = ATTR2_ALPHA(1)|sprites[obj_number].gfxID;
MoveSprite(&sprites[obj_number]);
obj_number++;
}
}
}
}
void read_width_height(u16 level_no)
{
u16 position;
char* filein;
char* line;
char fname[128];
sprintf(fname, "/BREAK_OUT_MANIA/levels/level%d.txt", level_no);
readfile(fname,&filein);
readline(filein,&line);
position=instr(line,",",0);
levels[level_no].tilewidth=atol(mid(line,0,position));
levels[level_no].tileheight=atol(mid(line,position+1,len(line)));
free(line);
readline(filein,&line);
position=instr(line,",",0);
levels[level_no].width=atol(mid(line,0,position));
levels[level_no].height=atol(mid(line,position+1,len(line)));
free(line);
closefile(filein);
}
u16 instr(char* name,char* seek_char,u16 startpos)
{
char *start = name+startpos;
return (u16) (strstr(start, seek_char)-start);
}
u16 start_pos;
u16 end_pos;
void readline(char* filehandle,char** buffer)
{
start_pos=end_pos;
u16 temp_instr_point=instr(filehandle,"\n",start_pos);
if (temp_instr_point)
{
end_pos=end_pos+temp_instr_point;
filehandle[end_pos]='\0';
*buffer=(char*) malloc (end_pos-start_pos+1);
strncpy(*buffer, filehandle+start_pos, temp_instr_point+1);
end_pos++;
}
}
void readfile(char* filename,char** filein)
{
FAT_InitFiles();
int handle = FAT_fopen(filename, "r");
int size=FAT_GetFileSize();
*filein = (char*) malloc (size+1);
for (int x=0 ; x<size ;x++)
{
(*filein)[x]=FAT_fgetc(handle);
}
FAT_fclose(handle);
}
void closefile(char* filename)
{
free(filename);
end_pos=0;
start_pos=0;
}
char tmp[200];
char* mid(char *src, u16 pos, u16 siz)
{
strncpy(tmp, src+pos, siz);
tmp[siz]='\0';
return tmp;
}
u16 len(char* stri)
{
return strlen(stri);
}
void draw_text(char* dtext,u16 tst,u16 ist)
{
for (int xst=0 ; xst<len(dtext) ; xst++)
draw_anim_image(vram , text , tst+(xst*8) , ist , 8 , 8 , (u16)dtext[xst]-32 , 632);
} |