diff --git a/.gitignore b/.gitignore index cd42ee3..7d85c72 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ bin/ obj/ +*.cbp +*.depend +*.layout diff --git a/App.cpp b/App.cpp new file mode 100644 index 0000000..a5596e7 --- /dev/null +++ b/App.cpp @@ -0,0 +1,10 @@ +#include "App.h" + +namespace App +{ + /// Application Main Method + void Main() + { + mlog("In App::Main()"); + } +} diff --git a/App.h b/App.h new file mode 100644 index 0000000..bd65162 --- /dev/null +++ b/App.h @@ -0,0 +1,9 @@ +#pragma once +#include "mini_engine.h" + +namespace App +{ + /** Entrance of Application. + */ + void Main(); +} diff --git a/InitManager.cpp b/InitManager.cpp new file mode 100644 index 0000000..935f5f1 --- /dev/null +++ b/InitManager.cpp @@ -0,0 +1,112 @@ +#include "InitManager.h" + +InitManager_SDL::InitManager_SDL() +{ + if(SDL_Init(SDL_INIT_EVERYTHING)<0) + { + Global::ErrorQuit("Failed to init SDL2."); + } +} + +InitManager_SDL::~InitManager_SDL() +{ + SDL_Quit(); +} + +InitManager_IMG::InitManager_IMG() +{ + if(IMG_Init(IMG_INIT_JPG|IMG_INIT_PNG)<0) + { + Global::ErrorQuit("Failed to init SDL2 Image."); + } +} + +SDL_Surface* InitManager_IMG::loadimage(const char* Filename) +{ + return IMG_Load(Filename); +} + +SDL_Texture* InitManager_IMG::loadtexture(SDL_Renderer* rnd,const char* Filename) +{ + return IMG_LoadTexture(rnd,Filename); +} + +InitManager_IMG::~InitManager_IMG() +{ + IMG_Quit(); +} + +int InitManager_TTF::ctm=0; + +InitManager_TTF::InitManager_TTF() +{ + /// ~_~ check ctm and add it anyway + if(ctm++>0) + { + return; + } + + if(TTF_Init()<0) + { + Global::ErrorQuit("Failed to init SDL2 TTF."); + } + _font=NULL; +} + +int InitManager_TTF::openFont(const char* FileName,int Size) +{ + _font=TTF_OpenFont(FileName,Size); + if(_font==NULL) return -1; + else return 0; +} + +TTF_Font* InitManager_TTF::font() +{ + return _font; +} + +int InitManager_TTF::closeFont() +{ + TTF_CloseFont(_font); + _font=NULL; + return 0; +} + +InitManager_TTF::~InitManager_TTF() +{ + /// Close Font anyway. + if(_font) closeFont(); + + /// ~_~ Firstly ctm=ctm-1, if ctm still > 0, then return ( TTF_Quit Not Executed ) + if(--ctm>0) + { + return; + } + + TTF_Quit(); +} + +InitManager_Mix::InitManager_Mix() +{ + if(Mix_Init(MIX_INIT_MP3)<0) + { + Global::ErrorQuit("Failed to Init Mixer."); + } + + if(Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 1024)<0) + { + Global::ErrorQuit("Failed to OpenAudio"); + } + Mix_AllocateChannels(32); +} + +InitManager_Mix::~InitManager_Mix() +{ + Mix_CloseAudio(); + Mix_Quit(); +} + +InitManager_SDL syssdl; +InitManager_IMG sysimg; +InitManager_TTF systtf; +InitManager_Mix sysmix; diff --git a/InitManager.h b/InitManager.h new file mode 100644 index 0000000..ca98d37 --- /dev/null +++ b/InitManager.h @@ -0,0 +1,44 @@ +#pragma once +#include "config.h" +#include + +class InitManager_SDL +{ +public: + InitManager_SDL(); + ~InitManager_SDL(); +}; + +class InitManager_TTF +{ +public: + InitManager_TTF(); + ~InitManager_TTF(); + int openFont(const char* FileName,int Size); + int closeFont(); + TTF_Font* font(); +private: + TTF_Font* _font; + static int ctm; +}; + +class InitManager_IMG +{ +public: + InitManager_IMG(); + ~InitManager_IMG(); + SDL_Surface* loadimage(const char* FileName); + SDL_Texture* loadtexture(SDL_Renderer* rnd,const char* Filename); +}; +class InitManager_Mix +{ +public: + InitManager_Mix(); + ~InitManager_Mix(); +}; + +extern InitManager_SDL syssdl; +extern InitManager_IMG sysimg; +extern InitManager_TTF systtf; +extern InitManager_Mix sysmix; + diff --git a/WindowManager.cpp b/WindowManager.cpp new file mode 100644 index 0000000..7c9e505 --- /dev/null +++ b/WindowManager.cpp @@ -0,0 +1,83 @@ +#include "WindowManager.h" + +WindowManager* default_WindowManager=nullptr; + +struct WindowManager::impl +{ + SDL_Renderer* rnd; + SDL_Window* wnd; + int winw,winh; +}; + +WindowManager::WindowManager() +{ + pimpl=new impl; + Init(); +} + +WindowManager* WindowManager::getDefaultInstance() /// static +{ + if(default_WindowManager) return default_WindowManager; + default_WindowManager=new WindowManager; + return default_WindowManager; +} +int WindowManager::releaseDefaultInstance() /// static +{ + if(!default_WindowManager) return -1; + delete default_WindowManager; + default_WindowManager=nullptr; + return 0; +} + +void WindowManager::Init() +{ + mlog("Called WindowSizeManager::Init()"); + if(pimpl->wnd!=NULL) return; + pimpl->wnd=SDL_CreateWindow("MiniEngine",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,1024,768,SDL_WINDOW_SHOWN); + updateSize(); + pimpl->rnd=SDL_CreateRenderer(pimpl->wnd,-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE); +} + +void WindowManager::Quit() +{ + mlog("Called WindowSizeManager::Quit()"); + if(pimpl->rnd==NULL) return; + + SDL_DestroyRenderer(pimpl->rnd); + pimpl->rnd=nullptr; + SDL_DestroyWindow(pimpl->wnd); + pimpl->wnd=nullptr; + + pimpl->winw=pimpl->winh=0; +} + +WindowManager::~WindowManager() +{ + Quit(); + delete pimpl; +} + +int WindowManager::getwinw() +{ + return pimpl->winw; +} + +int WindowManager::getwinh() +{ + return pimpl->winh; +} + +SDL_Window* WindowManager::getWindow() +{ + return pimpl->wnd; +} + +SDL_Renderer* WindowManager::getRender() +{ + return pimpl->rnd; +} + +void WindowManager::updateSize() +{ + SDL_GetWindowSize(pimpl->wnd,&pimpl->winw,&pimpl->winh); +} diff --git a/WindowManager.h b/WindowManager.h new file mode 100644 index 0000000..dd5af62 --- /dev/null +++ b/WindowManager.h @@ -0,0 +1,23 @@ +#pragma once +#include "config.h" + +class WindowManager +{ +public: + WindowManager(); + ~WindowManager(); + int getwinw(); + int getwinh(); + SDL_Renderer* getRender(); + SDL_Window* getWindow(); + void updateSize(); + + static WindowManager* getDefaultInstance(); + static int releaseDefaultInstance(); +protected: + void Init(); + void Quit(); +private: + struct impl; + impl* pimpl; +}; diff --git a/basic_config.cpp b/basic_config.cpp new file mode 100644 index 0000000..7439e16 --- /dev/null +++ b/basic_config.cpp @@ -0,0 +1,3 @@ +#include "basic_config.h" + +FILE* _log_fp=NULL; diff --git a/basic_config.h b/basic_config.h new file mode 100644 index 0000000..314fa86 --- /dev/null +++ b/basic_config.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include + +using namespace std; + +/// If you have configured SDL2 header files, +/// they will be used first. + +#include "SDL2/SDL.h" +#undef main + +#include "SDL2/SDL_ttf.h" +#include "SDL2/SDL_image.h" +#include "SDL2/SDL_Mixer.h" + +class NonCopyable +{ +public: + NonCopyable()=default; + ~NonCopyable()=default; + NonCopyable(const NonCopyable&)=delete; + NonCopyable& operator = (const NonCopyable&)=delete; +}; + +extern FILE* _log_fp; + +#ifdef _WINDOW_PROGRAM +#define mlog_init() _log_fp=fopen("mini_engine_log.txt","w") +#define mlog(fmt,args...) do {if(_log_fp) {fprintf(_log_fp,fmt,##args);fprintf(_log_fp,"\n");fflush(_log_fp);} }while(0) +#else +#define mlog_init() +#define mlog(fmt,args...) printf(fmt,##args);printf("\n") +#endif diff --git a/config.cpp b/config.cpp new file mode 100644 index 0000000..e28b8ab --- /dev/null +++ b/config.cpp @@ -0,0 +1,10 @@ +#include "config.h" + +namespace Global +{ + void ErrorQuit(const char* ErrorMessage) + { + mlog(ErrorMessage); + exit(0); + } +} diff --git a/config.h b/config.h new file mode 100644 index 0000000..5ac9cc3 --- /dev/null +++ b/config.h @@ -0,0 +1,8 @@ +#pragma once +#include "basic_config.h" + +#include "sdl_engine.h" +namespace Global +{ + void ErrorQuit(const char* ErrorMessage); +} diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..df57d2a --- /dev/null +++ b/main.cpp @@ -0,0 +1,12 @@ +#include "config.h" +#include "App.h" + +int main() +{ + mlog_init(); + /// Create The Default Window Instance + WindowManager::getDefaultInstance(); + App::Main(); + WindowManager::releaseDefaultInstance(); + return 0; +} diff --git a/mini_engine.cpp b/mini_engine.cpp new file mode 100644 index 0000000..d43cc19 --- /dev/null +++ b/mini_engine.cpp @@ -0,0 +1 @@ +#include "mini_engine.h" diff --git a/mini_engine.h b/mini_engine.h new file mode 100644 index 0000000..0efecbd --- /dev/null +++ b/mini_engine.h @@ -0,0 +1,14 @@ +#pragma once + +#include "config.h" + +/******************************************************************************/ +/// InitManager +#include "InitManager.h" + +/// Window Manager +#include "WindowManager.h" + +/******************************************************************************/ +/// Application +#include "App.h" diff --git a/sdl_engine.cpp b/sdl_engine.cpp new file mode 100644 index 0000000..77e1f4d --- /dev/null +++ b/sdl_engine.cpp @@ -0,0 +1,73 @@ +#include "sdl_engine.h" +#include "unistd.h" + +SDL_Texture* RenderText(SDL_Renderer* rnd,TTF_Font* font,const char* Text,SDL_Color color,int* pw,int* ph) +{ + SDL_Surface* surf=TTF_RenderText_Blended(font,Text,color); + if(surf==NULL) return NULL; + SDL_Texture* texture=SDL_CreateTextureFromSurface(rnd,surf); + SDL_FreeSurface(surf); + if(pw&&ph) SDL_QueryTexture(texture,NULL,NULL,pw,ph); + return texture; +} + +SDL_Texture* RenderUTF8(SDL_Renderer* rnd,TTF_Font* font,const char* Text,SDL_Color color,int* pw,int* ph) +{ + SDL_Surface* surf=TTF_RenderUTF8_Blended(font,Text,color); + if(surf==NULL) return NULL; + SDL_Texture* texture=SDL_CreateTextureFromSurface(rnd,surf); + SDL_FreeSurface(surf); + if(pw&&ph) SDL_QueryTexture(texture,NULL,NULL,pw,ph); + return texture; +} + +void TextureDraw(SDL_Renderer* rnd,SDL_Texture* texture,int dstx,int dsty) +{ + if(!rnd||!texture) return; + int w,h; + SDL_QueryTexture(texture,NULL,NULL,&w,&h); + SDL_Rect rect; + rect.x=dstx; + rect.y=dsty; + rect.w=w; + rect.h=h; + SDL_RenderCopy(rnd,texture,NULL,&rect); +} + +SDL_Texture* MyLoadImage(SDL_Renderer* rnd,const char* FileName,int* pw,int* ph) +{ + SDL_Surface* surf=IMG_Load(FileName); + if(surf==NULL) return NULL; + SDL_Texture* texture=SDL_CreateTextureFromSurface(rnd,surf); + SDL_FreeSurface(surf); + if(texture==NULL) return NULL; + if(pw&&ph) SDL_QueryTexture(texture,NULL,NULL,pw,ph); + return texture; +} + +bool isInRect(int x,int y,SDL_Rect rect) +{ + return ((x>=rect.x&&x<=rect.x+rect.w)&&(y>=rect.y&&y<=rect.y+rect.h)); +} + +bool isInRect(int x,int y,int LU_x,int LU_y,int RD_x,int RD_y) +{ + return ((x>=LU_x&&x<=RD_x)&&(y>=LU_y&&y<=RD_y)); +} + +void ClearMessageQueue() +{ + /// Clear Message Queue + while(SDL_PeepEvents(NULL,1,SDL_GETEVENT,SDL_FIRSTEVENT,SDL_LASTEVENT)); +} + +SDL_Color color_white { 255,255,255 }; +SDL_Color color_black { 0,0,0 }; + +int MyChangeDir(const char* DirName) +{ + mlog("Change Dir to \"%s\"",DirName); + int ret=chdir(DirName); + mlog("Change Dir returns %d",ret); + return ret; +} diff --git a/sdl_engine.h b/sdl_engine.h new file mode 100644 index 0000000..b8aecf3 --- /dev/null +++ b/sdl_engine.h @@ -0,0 +1,15 @@ +#pragma once +#include "basic_config.h" + +SDL_Texture* RenderText(SDL_Renderer* rnd,TTF_Font* font,const char* Text,SDL_Color color,int* pw,int* ph); +SDL_Texture* RenderUTF8(SDL_Renderer* rnd,TTF_Font* font,const char* Text,SDL_Color color,int* pw,int* ph); +void TextureDraw(SDL_Renderer* rnd,SDL_Texture* texture,int dstx,int dsty); +SDL_Texture* MyLoadImage(SDL_Renderer* rnd,const char* FileName,int* pw,int* ph); +bool isInRect(int x,int y,SDL_Rect rect); +bool isInRect(int x,int y,int LU_x,int LU_y,int RD_x,int RD_y); +void ClearMessageQueue(); + +int MyChangeDir(const char* DirName); + +extern SDL_Color color_white; +extern SDL_Color color_black;