1 /*
2     Copyright (c) 2009 Andrew Caudwell (acaudwell@gmail.com)
3     All rights reserved.
4 
5     Redistribution and use in source and binary forms, with or without
6     modification, are permitted provided that the following conditions
7     are met:
8     1. Redistributions of source code must retain the above copyright
9        notice, this list of conditions and the following disclaimer.
10     2. Redistributions in binary form must reproduce the above copyright
11        notice, this list of conditions and the following disclaimer in the
12        documentation and/or other materials provided with the distribution.
13     3. The name of the author may not be used to endorse or promote products
14        derived from this software without specific prior written permission.
15 
16     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef SDLAPP_H
29 #define SDLAPP_H
30 
31 #ifdef _WIN32
32 
33 #ifdef _WIN32_WINNT
34 #undef _WIN32_WINNT
35 #endif
36 
37 #define _WIN32_WINNT 0x0501
38 #include "windows.h"
39 #endif
40 
41 #include "gl.h"
42 
43 #include <string>
44 #include <exception>
45 #include <vector>
46 
47 extern std::string gSDLAppConfDir;
48 extern std::string gSDLAppResourceDir;
49 extern std::string gSDLAppPathSeparator;
50 
51 extern std::string gSDLAppTitle;
52 extern std::string gSDLAppExec;
53 
54 void SDLAppInfo(std::string msg);
55 void SDLAppQuit(std::string error);
56 
57 void SDLAppInit(std::string apptitle, std::string execname, std::string exepath = "");
58 bool SDLAppDirExists(std::string dir);
59 std::string SDLAppAddSlash(std::string path);
60 
61 void SDLAppParseArgs(int argc, char *argv[], int* xres, int* yres, bool* fullscreen, std::vector<std::string>* otherargs = 0);
62 
63 class SDLAppException : public std::exception {
64 protected:
65     std::string message;
66     bool showhelp;
67 
68 public:
SDLAppException(const char * str,...)69     SDLAppException(const char* str, ...) : showhelp(false) {
70 
71         va_list vl;
72         char msg[65536];
73 
74         va_start(vl, str);
75             vsnprintf(msg, 65536, str, vl);
76         va_end(vl);
77 
78         message = std::string(msg);
79     }
80 
SDLAppException(std::string message)81     SDLAppException(std::string message) : showhelp(false), message(message) {}
82 
throw()83     ~SDLAppException() throw () {};
84 
showHelp()85     bool showHelp() const { return showhelp; }
setShowHelp(bool showhelp)86     void setShowHelp(bool showhelp) { this->showhelp = showhelp; };
87 
what()88     virtual const char* what() const throw() { return message.c_str(); }
89 };
90 
91 class SDLApp {
92     int frame_count;
93     int fps_updater;
94     int return_code;
95 
96     void updateFramerate();
97 protected:
98     int  min_delta_msec;
99     bool appFinished;
100 
101     void stop(int return_code);
102 
103     virtual bool handleEvent(SDL_Event& event);
104 public:
105     float fps;
106 
107     SDLApp();
~SDLApp()108     virtual ~SDLApp() {};
109 
110 #ifdef USE_X11
111     static void initX11ClipboardEventFilter();
112     static int X11ClipboardEventFilter(const SDL_Event *event);
113 #endif
114 
115 #ifdef _WIN32
116     static HWND console_window;
117     static bool existing_console;
118 
119     static void initConsole();
120     static void showConsole(bool show);
121     static void resizeConsole(int height);
122 #endif
123 
124     int run();
125 
126     static bool getClipboardText(std::string& text);
127     static void setClipboardText(const std::string& text);
128 
resize(int width,int height)129     virtual void resize(int width, int height) {};
130 
update(float t,float dt)131     virtual void update(float t, float dt) {};
init()132     virtual void init() {};
133 
logic(float t,float dt)134     virtual void logic(float t, float dt) {};
draw(float t,float dt)135     virtual void draw(float t, float dt) {};
136 
quit()137     virtual void quit() { appFinished = true; };
138 
mouseMove(SDL_MouseMotionEvent * e)139     virtual void mouseMove(SDL_MouseMotionEvent *e) {};
mouseClick(SDL_MouseButtonEvent * e)140     virtual void mouseClick(SDL_MouseButtonEvent *e) {};
keyPress(SDL_KeyboardEvent * e)141     virtual void keyPress(SDL_KeyboardEvent *e) {};
142 
143 #if SDL_VERSION_ATLEAST(2,0,0)
textInput(SDL_TextInputEvent * e)144     virtual void textInput(SDL_TextInputEvent* e)  {};
textEdit(SDL_TextEditingEvent * e)145     virtual void textEdit(SDL_TextEditingEvent* e) {};
146 
mouseWheel(SDL_MouseWheelEvent * e)147     virtual void mouseWheel(SDL_MouseWheelEvent *e) {};
148 #endif
149 
150     int returnCode();
151     bool isFinished();
152 };
153 
154 #endif
155