⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 nds上的电子书软件
💻 C
字号:

#include <PA9.h>  // PAlib include
#include "hz.h"   //中文显示
#include <stdio.h>
#include <stdlib.h>

#include <fat.h>

#include <sys/dir.h>


static u8 *TextBuffer = NULL;
static u8 *LstBuffer = NULL;
static u16 bgColor=PA_RGB(31,31,31);
static u16 fontColor=PA_RGB(0,0,0);

static u32 curNode=1;
static int txtOffset=0;
static int page=0;


static int totalNodeNum=0;

static char fileName[256];

//static char pageNum[256];

//static FILE* bookmarkFile=NULL;
void saveU32(u32 u32ToSave,char* fileName)
{
	char tempStr[256];
	sprintf(tempStr,"%d",u32ToSave);
	//iprintf("beforsave\nfilename:%s\nu32ToSave:%sstrlen:%d\n",fileName,tempStr,strlen(tempStr)+1);
	FILE* savFile = fopen (fileName, "w");
	fwrite(tempStr,1,strlen(tempStr)+1,savFile); 
	fclose(savFile);
	//iprintf("aftersave\nfilename:%s\nu32ToSave:%sstrlen:%d\n",fileName,tempStr,strlen(tempStr)+1);
}
u32 readU32(char* fileName)
{
	char tempStr[256];
	//iprintf("beforread\nfilename:%s\n",fileName);
	FILE* savFile = fopen (fileName, "r");
	fscanf(savFile,"%s",tempStr);
	fclose(savFile);
	//iprintf("afterread\nfilename:%s\nu32ToRead:%sstrlen:%d",fileName,tempStr,strlen(tempStr)+1);
	return (u32)atoi(tempStr);
}

u16 u16GetValue(u8 *buffer)
{
	return ((0x0000|*(buffer+1))<<8)+(*buffer);
	//return ((0x0000|0xfd)<<8)+(0xef);
}
int intGetValue(u8 *buffer)
{
	return ((0x00000000|*(buffer+3))<<24)+((0x000000|*(buffer+2))<<16)+((0x0000|*(buffer+1))<<8)+(*buffer);
}


u8 *GetTextBuffer(u8 *lstbuf)
{
	
	strcpy(fileName,lstbuf);
	char *fn,*extfn;
	extfn=strchr(fileName,'.')+1;
	*(extfn-1)='\0';
	fn=fileName;	
	return PA_PAFSFile(PA_FSGetFile(0,fn,extfn));
}

void ShowText()
{
	char pagenum[32];
	int w,h; 
	//或取当前显示的文本
	txtOffset=intGetValue(LstBuffer+264+(curNode-1)*8);
	page=intGetValue(LstBuffer+264+(curNode-1)*8+4);
	//PA_OutputText(1, 0, i+2, "%d,%d", page,txtOffset);
				
	for(h=0;h<192;h++)
		for(w=0;w<256;w++)
		{
			//PA_Put16bitPixel(1, w, h,PA_RGB(0,0,31));
			PA_Put16bitPixel(1, w, h,bgColor);
		}
	SetFontA();
	OutputGbkTextV(1, SCREEN_WIDTH, 0, fontColor, TextBuffer+txtOffset, 16);//显示中文
	sprintf(pagenum,"- %d - 共%d页",page,totalNodeNum);
	SetFontB();
	OutputGbkTextV(1, 12, 5, fontColor,pagenum, 12);//显示中文
	PA_Draw16bitLine(1,15,5,15,SCREEN_HEIGHT-5,fontColor);
	/////////////////////////////////////////////
	//下屏
	/////////////////////////////////////////////
	//curNode++;
	//或取当前显示的文本
	for(h=0;h<192;h++)
		for(w=0;w<256;w++)
		{
			//PA_Put16bitPixel(1, w, h,PA_RGB(0,0,31));
			PA_Put16bitPixel(0, w, h,bgColor);
		}
	if((curNode+1)<=totalNodeNum)
	{
		txtOffset=intGetValue(LstBuffer+264+(curNode)*8);
		page=intGetValue(LstBuffer+264+(curNode)*8+4);
		//PA_OutputText(1, 0, i+2, "%d,%d", page,txtOffset);
					
		
		SetFontA();
		OutputGbkTextV(0, SCREEN_WIDTH, 0, fontColor, TextBuffer+txtOffset, 16);//显示中文
		sprintf(pagenum,"- %d -",page);
		SetFontB();
		OutputGbkTextV(0, 13, SCREEN_HEIGHT-strlen(pagenum)*6-5, fontColor,pagenum, 12);//显示中文
		PA_Draw16bitLine(0,15,5,15,SCREEN_HEIGHT-5,fontColor);
	}
}
int main(void)	{

	// PAlib Inits
	PA_Init();
	PA_InitVBL();
	
	// 初始化上下屏
	PA_Init16bitBg(1,0);
	PA_Init16bitBg(0,0);
	
	PA_InitText(0, 0);  // Initialise the text system on the bottom screen	
	PA_InitText(1, 0);  // Initialise the text system on the top screen

	//PA_OutputSimpleText(1,1,1,"Hello World !"); // Top screen, tile x = 1, y = 1...
	
	//debug
	//u32 FileNumber = 0;
	//FileNumber = 
	PA_FSInit(); // Inits PA File System, and returns the number of files
	//PA_OutputText(1, 0, 0, "  Files   : %d    ", FileNumber);
	
	//s32 i;	
	//for (i = 0; i < FileNumber; i++){
	//	PA_OutputText(0, 0, i, "%s.%s   Size : %d, %d", PA_FSFile[i].Name, PA_FSFile[i].Ext, PA_FSFile[i].Length, PA_FSFile[i].FilePos);
	//}


  	//建立文件系统
	if(!fatInitDefault())
		PA_OutputSimpleText(1,0,2,"fatInitDefault Error !");

	

	//读取中文字体
	if(!LoadGbkFontA("ASC16","fon","GBK16","fon"))
		PA_OutputSimpleText(1,0,3,"LoadGbkFontA Error !");
	
	
	if(!LoadGbkFontB("ASC12","fon","GBK12","fon"))
		PA_OutputSimpleText(1,0,4,"LoadGbkFontB Error !"); 
	

	//读取lst文件
	s32 idx;
	idx = PA_FSGetFile(0, "text", "lst");
	if(idx != -1)
	{ 
		LstBuffer = (u8 *)PA_PAFSFile(idx);
	}

	//设置常量
	bgColor=u16GetValue(LstBuffer+256);
	fontColor=u16GetValue(LstBuffer+258);
	totalNodeNum=u16GetValue(LstBuffer+260);

	//读取文本文件
	TextBuffer=GetTextBuffer(LstBuffer);
	ShowText();

	
	SetFontB();
	while(1) 	// Inifinite loop
	{  
		
		if(Pad.Newpress.Down)
		{
			if(curNode+1<totalNodeNum)
			{
				curNode+=2;
				
				ShowText();
			}
			
		}
		else if(Pad.Newpress.Up)
		{
			if(page>2)
			{
				curNode-=2;
				ShowText();
			}
		}
		else if(Pad.Newpress.L)
		{
			
			
			char savPath[256];
			sprintf(savPath,"/%s",fileName);
			saveU32(curNode,savPath);

			SetFontB();
			OutputGbkTextV(0, 13, 5, PA_RGB(31,0,0),"书签已保存!", 12);
			
			//PA_OutputText(1, 0, 4, "save curNode at: %d    ", curNode);
			
		}
		else if(Pad.Newpress.R)
		{
			
			
			char savPath[256];
			sprintf(savPath,"/%s",fileName);

			u32 tmpPage = readU32(savPath);

			if(tmpPage>=1&&tmpPage<=totalNodeNum)
			{
				curNode = tmpPage;
				ShowText();
			
				SetFontB();
				OutputGbkTextV(0, 13, 5, PA_RGB(31,0,0),"书签已读取!", 12);
			}
			
			//PA_OutputText(1, 0, 3, "load at curNode: %d    ", curNode);
			
			
			
			
		}
		
		PA_WaitForVBL();
	}

	
	
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -