diff options
Diffstat (limited to 'src/master')
-rw-r--r-- | src/master/init.cpp | 1 | ||||
-rw-r--r-- | src/master/intro.cpp | 202 | ||||
-rw-r--r-- | src/master/master.hpp | 64 | ||||
-rw-r--r-- | src/master/resources.cpp | 18 | ||||
-rw-r--r-- | src/master/resources.hpp | 7 | ||||
-rw-r--r-- | src/master/scenemgr.cpp | 63 |
6 files changed, 355 insertions, 0 deletions
diff --git a/src/master/init.cpp b/src/master/init.cpp new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/src/master/init.cpp @@ -0,0 +1 @@ + diff --git a/src/master/intro.cpp b/src/master/intro.cpp new file mode 100644 index 0000000..f8cf46b --- /dev/null +++ b/src/master/intro.cpp @@ -0,0 +1,202 @@ +#include "master.hpp" +#include "resources.hpp" +#include <cmath> +#include <cstdlib> +#include <ctime> +#include "../ui/menus.hpp" +#define XOFFSET 75 +#define YOFFSET 100 + +extern sceneManager *sceneMgr; +SMELT *introScene::sm=0; +bool introScene::sceneUpdate() +{ + bool done=bcnt>400; + for(int i=0;i<bcnt;++i) + { + pos[i]=pos[i]+vel[i]; + vel[i]=vel[i]+acc[i]; + if(pos[i].x>-40&&pos[i].x<1000&&pos[i].y>-40&&pos[i].y<800)done=false; + if(scale[i]>0.7)scale[i]-=0.1;else scale[i]=0.6; + } + if(done) + { + mainMenuScn=new mainMenuScene; + sceneMgr->activateScene("MainMenu"); + delete this; + } + delay+=sm->smGetDelta(); + if(delay<0.016)return false; + delay=0; + for(int cc=0;cc<3;++cc) + { + ++step; + if(ch==0)//C + { + float angle=(60-step)/45.*PI+PI/3.; + pos[bcnt]=smvec2d(100+XOFFSET+80*cos(angle),200+YOFFSET+80*sin(angle)); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + if(step>=60){++ch;step=0;} + } + if(ch==1)//h + { + if(step<=40) + { + pos[bcnt]=smvec2d(180+XOFFSET,120+YOFFSET+step*4); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + } + if(step>40&&step<70) + { + float angle=(step-40)/30.*PI+PI; + pos[bcnt]=smvec2d(210+XOFFSET+30*cos(angle),220+YOFFSET+30*sin(angle)); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + } + if(step>=70) + { + pos[bcnt]=smvec2d(240+XOFFSET,220+YOFFSET+(step-70)*4); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + if(step>=85){++ch;step=0;} + } + } + if(ch==2)//r + { + if(step<=20) + { + pos[bcnt]=smvec2d(280+XOFFSET,200+YOFFSET+step*4); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + } + if(step>20) + { + float angle=(step-20)/30.*PI+PI; + pos[bcnt]=smvec2d(310+XOFFSET+30*cos(angle),230+YOFFSET+30*sin(angle)); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + if(step>=45){++ch;step=0;} + } + } + if(ch==3)//i + { + if(step<=20) + { + pos[bcnt]=smvec2d(360+XOFFSET,200+YOFFSET+step*4); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + } + if(step>20) + { + pos[bcnt]=smvec2d(360+XOFFSET,180+YOFFSET); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + ++ch;step=0; + } + } + if(ch==4)//s + { + if(step<=30) + { + float angle=(30-step)/30.*PI+2*PI/3.; + pos[bcnt]=smvec2d(450+XOFFSET+40*cos(angle),170+YOFFSET+40*sin(angle)); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + } + if(step>30) + { + float angle=(step-30)/30.*PI+5*PI/3.; + pos[bcnt]=smvec2d(410+XOFFSET+40*cos(angle),239.282+YOFFSET+40*sin(angle)); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + if(step>60){++ch;step=0;} + } + } + if(ch==5) + { + float angle=-step/30.*PI+3*PI/2.; + pos[bcnt]=smvec2d(520+XOFFSET+40*cos(angle),240+YOFFSET+40*sin(angle)); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + if(step>60){++ch;step=0;} + } + if(ch==6) + { + if(step<=30) + { + float angle=(30-step)/30.*PI+PI; + pos[bcnt]=smvec2d(620+XOFFSET+30*cos(angle),160+YOFFSET+30*sin(angle)); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + } + if(step>30&&step<=55) + { + pos[bcnt]=smvec2d(590+XOFFSET,160+YOFFSET+(step-30)*5); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + } + if(step>55) + { + pos[bcnt]=smvec2d(570+XOFFSET+(step-55)*4,190+YOFFSET); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + if(step>75){++ch;step=0;} + } + } + if(ch==7) + { + if(step<=20) + { + pos[bcnt]=smvec2d(700+XOFFSET,160+YOFFSET+step*5); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + } + if(step>25&&step<=55) + { + float angle=(55-step)/30.*PI; + pos[bcnt]=smvec2d(730+XOFFSET+30*cos(angle),260+YOFFSET+30*sin(angle)); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + } + if(step>55) + { + pos[bcnt]=smvec2d(680+XOFFSET+(step-55)*4,190+YOFFSET); + vel[bcnt]=smvec2d(0,0);acc[bcnt]=smvec2d(0,0);scale[bcnt]=1.5;++bcnt; + if(step>75){++ch;step=0;} + } + } + if(ch==8) + {if(step>120){++ch;step=0;}} + if(ch==9) + { + for(int i=0;i<bcnt;++i) + { + do{acc[i]=smvec2d(rand()%100-50.,rand()%100-50.);acc[i].normalize(); + }while(acc[i].l()<1E-6); + acc[i]=(rand()%100/400.+0.2)*acc[i]; + } + ++ch; + } + if(ch==10) + {if(step>180){++ch;step=0;}} + } + return false; +} +bool introScene::sceneRender() +{ + if(ch!=11) + sm->smClrscr(0xFF000000); + else + {int c=step*4>255?255:step*4;sm->smClrscr(ARGB(255,c,c,c));} + for(int i=0;i<bcnt;++i) + blt->render(pos[i].x,pos[i].y,0,scale[i],scale[i]); + return false; +} +introScene::introScene() +{ + sm=smGetInterface(SMELT_APILEVEL); + pos=new smvec2d[1024];vel=new smvec2d[1024]; + scale=new float[1024];acc=new smvec2d[1024]; + extern const char* bsnames[]; + smTexInfo *ti=ssanm.getTextureInfo(bsnames[rand()%8]); + blt=new smEntity2D(ti->tex,ti->rect); + blt->setCentre(12,12); + blt->setColor(0xC0FFFFFF); + sceneMgr->registerScene(this,"Intro",1000); +} +void introScene::introInit() +{ + bcnt=step=ch=0; + sceneMgr->activateScene("Intro"); +} +introScene::~introScene() +{ + sceneMgr->deactivateScene("Intro"); + delete[] pos;delete[] vel;delete[] acc;delete[] scale;delete blt; + sm->smRelease(); +} diff --git a/src/master/master.hpp b/src/master/master.hpp new file mode 100644 index 0000000..8ccb740 --- /dev/null +++ b/src/master/master.hpp @@ -0,0 +1,64 @@ +#ifndef MASTER_H +#define MASTER_H +#include "smelt.hpp" +#include "smdatapack.hpp" +#include "smmath.hpp" +#include "smentity.hpp" +#include "smanim.hpp" +#include <thread> +#include <chrono> +struct pn{int p,n;}; +class scenePrototype +{ +public: + virtual bool sceneUpdate(){return false;} + virtual bool sceneRender(){return false;} + virtual bool threadUpdate(){return false;} + virtual ~scenePrototype(){} +}; +class sceneManager +{ +private: + scenePrototype *sps[256]; + static SMELT* sm; + static std::thread* uth; + char names[256][16]; + bool active[256],thactive; + int scnt,dlps,lp,utime; + float lps,lpsud; + pn pq[256]; +public: + sceneManager(); + ~sceneManager(); + int registerScene(scenePrototype *sp,const char* scenename,int priority); + void activateScene(const char* scene); + void deactivateScene(const char* scene); + void startUpdate(); + void stopUpdate(); + bool isThreadActive(); + int getSceneID(const char* scene); + bool isSceneActive(int sceneid); + void setLPS(int lps); + float getLPS(); + int getTHUpdateTime(); + bool update(); + void threadUpdate(); +}; + +class introScene:public scenePrototype +{ +private: + char* ssptr;DWORD sssez; + smvec2d *pos,*vel,*acc; + float *scale,delay; + smEntity2D *blt; + int bcnt,step,ch; + static SMELT *sm; +public: + introScene(); + ~introScene(); + void introInit(); + bool sceneUpdate()override; + bool sceneRender()override; +}; +#endif diff --git a/src/master/resources.cpp b/src/master/resources.cpp new file mode 100644 index 0000000..1ae35cf --- /dev/null +++ b/src/master/resources.cpp @@ -0,0 +1,18 @@ +#include "smdatapack.hpp" +#include "smbmfont.hpp" +smDtpFileR blrdtp; +smBMFont lcdfont; +smAnmFile ssanm; +void resourcesLoad() +{ + blrdtp.openDtp("blr3.dtp"); + lcdfont.loadAnmFromMemory(blrdtp.getFilePtr("lcdfont.anm"),blrdtp.getFileSize("lcdfont.anm")); + lcdfont.setScale(0.6); + ssanm.loadAnmFromMemory(blrdtp.getFilePtr("ss.anm"),blrdtp.getFileSize("ss.anm")); +} +void resourcesUnload() +{ + blrdtp.closeDtp(); + lcdfont.close(); + ssanm.close(); +} diff --git a/src/master/resources.hpp b/src/master/resources.hpp new file mode 100644 index 0000000..2cfb497 --- /dev/null +++ b/src/master/resources.hpp @@ -0,0 +1,7 @@ +#include "smdatapack.hpp" +#include "smbmfont.hpp" +extern smDtpFileR blrdtp; +extern smBMFont lcdfont; +extern smAnmFile ssanm; +extern void resourcesLoad(); +extern void resourcesUnload(); diff --git a/src/master/scenemgr.cpp b/src/master/scenemgr.cpp new file mode 100644 index 0000000..1399d63 --- /dev/null +++ b/src/master/scenemgr.cpp @@ -0,0 +1,63 @@ +#include "master.hpp" +#include <cstring> +#include <cstdlib> +int cmp(const void* a,const void* b){return ((pn*)b)->p-((pn*)a)->p;} +extern sceneManager *sceneMgr; +SMELT* sceneManager::sm=NULL; +std::thread* sceneManager::uth=NULL; +sceneManager::sceneManager(){sm=smGetInterface(SMELT_APILEVEL);utime=scnt=0;thactive=false;dlps=64;} +sceneManager::~sceneManager(){sm->smRelease();} +int sceneManager::registerScene(scenePrototype *sp,const char* scenename,int priority) +{ + for(int i=0;i<scnt;++i)if(!strncmp(scenename,names[i],16))return -1; + sps[scnt]=sp; + strncpy(names[scnt],scenename,16); + active[scnt]=false; + pq[scnt].p=priority;pq[scnt].n=scnt;++scnt; + qsort(pq,scnt,sizeof(pn),cmp); + return scnt-1; +} +void sceneManager::activateScene(const char* scene) +{for(int i=0;i<scnt;++i)if(!strncmp(scene,names[i],16)){active[i]=true;break;}} +void sceneManager::deactivateScene(const char* scene) +{for(int i=0;i<scnt;++i)if(!strncmp(scene,names[i],16)){active[i]=false;break;}} +void sceneManager::startUpdate(){thactive=true;uth=new std::thread(&sceneManager::threadUpdate,sceneMgr);} +void sceneManager::stopUpdate(){thactive=false;uth->join();delete uth;} +bool sceneManager::isThreadActive(){return thactive;} +int sceneManager::getSceneID(const char* scene) +{for(int i=0;i<scnt;++i)if(!strncmp(scene,names[i],16))return i;return -1;} +bool sceneManager::isSceneActive(int sceneid){return active[sceneid];} +void sceneManager::setLPS(int lps){dlps=lps;} +float sceneManager::getLPS(){return lps;} +int sceneManager::getTHUpdateTime(){return utime;} +//Update functions return true if it wants to terminate the application. +//Render functions should never return true. +bool sceneManager::update() +{ + for(int i=0;i<scnt;++i) + if(active[pq[i].n])if(sps[pq[i].n]->sceneUpdate())return true; + sm->smRenderBegin2D(true); + for(int i=0;i<scnt;++i) + if(active[pq[i].n])sps[pq[i].n]->sceneRender(); + sm->smRenderEnd(); + if(sm->smGetKeyState(SMK_S)==SMKST_HIT)sm->smScreenShot("ss0.bmp"); + return false; +} +void sceneManager::threadUpdate() +{ + long ddlay; + while(thactive) + { + ddlay=1000000000/dlps; + using namespace std::chrono; + high_resolution_clock::time_point b=high_resolution_clock::now(); + for(int i=0;i<scnt;++i) + if(active[pq[i].n])sps[pq[i].n]->threadUpdate(); + high_resolution_clock::time_point a=high_resolution_clock::now(); + auto updateTime=a-b;utime=updateTime.count(); + ++lp;lpsud+=updateTime.count()>ddlay?updateTime.count():ddlay; + if(lpsud>=1000000000){lps=(float)lp/(lpsud/1000000000.);lp=0;lpsud=0;} + if(updateTime<(duration<long,std::ratio<1l,1000000000l>>)ddlay) + std::this_thread::sleep_for((duration<long,std::ratio<1l,1000000000l>>)ddlay-updateTime); + } +} |