Initial Commit.

This commit is contained in:
unknown 2017-01-03 23:32:35 +08:00
parent 6c2cde98e4
commit 5265b690a7
16 changed files with 456 additions and 0 deletions

3
.gitignore vendored
View File

@ -1,2 +1,5 @@
bin/ bin/
obj/ obj/
*.cbp
*.depend
*.layout

10
App.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "App.h"
namespace App
{
/// Application Main Method
void Main()
{
mlog("In App::Main()");
}
}

9
App.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include "mini_engine.h"
namespace App
{
/** Entrance of Application.
*/
void Main();
}

112
InitManager.cpp Normal file
View File

@ -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;

44
InitManager.h Normal file
View File

@ -0,0 +1,44 @@
#pragma once
#include "config.h"
#include <vector>
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;

83
WindowManager.cpp Normal file
View File

@ -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);
}

23
WindowManager.h Normal file
View File

@ -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;
};

3
basic_config.cpp Normal file
View File

@ -0,0 +1,3 @@
#include "basic_config.h"
FILE* _log_fp=NULL;

36
basic_config.h Normal file
View File

@ -0,0 +1,36 @@
#pragma once
#include <cstdio>
#include <cstdlib>
#include <cstring>
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

10
config.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "config.h"
namespace Global
{
void ErrorQuit(const char* ErrorMessage)
{
mlog(ErrorMessage);
exit(0);
}
}

8
config.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include "basic_config.h"
#include "sdl_engine.h"
namespace Global
{
void ErrorQuit(const char* ErrorMessage);
}

12
main.cpp Normal file
View File

@ -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;
}

1
mini_engine.cpp Normal file
View File

@ -0,0 +1 @@
#include "mini_engine.h"

14
mini_engine.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include "config.h"
/******************************************************************************/
/// InitManager
#include "InitManager.h"
/// Window Manager
#include "WindowManager.h"
/******************************************************************************/
/// Application
#include "App.h"

73
sdl_engine.cpp Normal file
View File

@ -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;
}

15
sdl_engine.h Normal file
View File

@ -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;