Added files with translations to several languages:
[hex-a-hop.git] / state.h
blob37f98ded8011ce5beeff7fd63ec6a73ec49c1e57
1 /*
2 Copyright (C) 2005-2007 Tom Beaumont
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 // Config block
25 // Uncomment this to check cross-platform compilation compatibility
26 // #undef WIN32
28 //#define USE_BBTABLET
29 //#define USE_OPENGL
31 #define SCREEN_W 640
32 #define SCREEN_H 480
35 // End of config block
38 // Hacky workaround for MSVC's broken for scoping
39 #define for if (0) ; else for
41 #include <SDL/SDL.h>
42 #ifdef USE_OPENGL
43 #include <SDL/SDL_OpenGL.h>
44 #endif
45 #include <math.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <stdarg.h>
51 extern SDL_Surface * screen;
53 #ifdef WIN32
54 // Trigger debugger
55 // #define FATAL(string, string2) do{__asm{int 3};}while(0)
56 static inline void FATAL(const char * string="Unknown", const char * string2="") { fprintf(stderr, "Fatal error: %s \"%s\"\n", string, string2); exit(0); }
57 #else
58 static inline void FATAL(const char * string="Unknown", const char * string2="") { fprintf(stderr, "Fatal error: %s \"%s\"\n", string, string2); exit(0); }
59 #endif
61 class String
63 int len;
64 char* data;
65 public:
66 void reserve(int i) { if (i<0) FATAL("-ve string length."); if (i<=len) return; len=i; data=(char*)realloc(data, (len+1)*sizeof(char)); }
67 String() : len(0), data(NULL) {}
68 String(String const & s) : len(0), data(NULL) { reserve(s.len); strcpy(data, s.data); }
69 ~String() { free(data); }
70 operator const char* () const {return data ? data : "";}
71 void operator = (String const & a) { *this = (const char*)a; }
72 void operator = (const char * a) { reserve(strlen(a)); strcpy(data, a); }
73 void operator += (const char * a) { reserve(strlen(a)+len); strcat(data, a); }
74 void truncate (int pos) { data[pos] = '\0'; }
75 void fix_backslashes() { if(data) for (int i=0; data[i]; i++) if (data[i]=='\\') data[i]='/'; }
78 class State
80 public:
81 virtual ~State() {}
83 virtual bool KeyPressed(int key, int mod) = 0;
84 virtual void KeyReleased(int key) {};
85 virtual void Mouse(int x, int y, int dx, int dy, int buttons_pressed, int buttons_released, int buttons) = 0;
86 virtual void Update(double timedelta) = 0;
87 virtual void Render() = 0;
88 virtual void FileDrop(const char* filename) = 0;
89 virtual void ScreenModeChanged() {};
92 /************************************************************************
93 // TEMPLATE - copy & paste
95 #define ClassName NEWSTATE
96 class ClassName : public State
98 public:
99 virtual bool KeyPressed(int key, int mod)
101 return false;
103 virtual void KeyReleased(int key)
106 virtual void Mouse(int x, int y, int dx, int dy, int buttons_pressed, int buttons_released, int buttons)
109 virtual void FileDrop(const char* filename)
112 virtual void Update(double timedelta)
114 // TODO
116 virtual void Render()
118 // TODO
120 virtual void ScreenModeChanged()
122 // TODO
125 MAKE_STATE(ClassName, _KEY_, false);
127 ************************************************************************/
129 class StateMakerBase
131 static StateMakerBase* first;
132 StateMakerBase* next;
133 int key;
134 bool start;
135 protected:
136 State* state;
137 public:
138 static State* current;
140 public:
141 StateMakerBase(int key, bool start) : state(NULL)
143 for (StateMakerBase* s = first; s; s=s->next)
144 if(key == s->key)
146 FATAL("Duplicate key in StateMakerBase::StateMakerBase");
147 return;
149 this->key = key;
150 this->start = start;
151 next = first;
152 first = this;
154 virtual State* Create() = 0;
155 void Destroy()
157 delete state;
158 state = 0;
160 static State* GetNew(int k)
162 for (StateMakerBase* s = first; s; s=s->next)
163 if(k==s->key)
164 return current = s->Create();
165 return current;
167 static State* GetNew()
169 if (!first)
171 FATAL("StateMakerBase::GetNew - first is NULL");
172 return 0;
174 for (StateMakerBase* s = first; s; s=s->next)
175 if(s->start)
176 return current = s->Create();
177 return current = first->Create();
179 static void DestroyAll()
181 for (StateMakerBase* s = first; s; s=s->next)
182 s->Destroy();
183 current = 0;
187 template<class X>
188 class StateMaker : public StateMakerBase
190 public:
191 StateMaker(int key, bool start=false) : StateMakerBase(key, start)
193 State* Create()
195 if (!state) state = new X;
196 return state;
200 #define MAKE_STATE(x,key,start) static StateMaker<x> _maker_##x(key, start);
202 extern int mouse_buttons, mousex, mousey, noMouse;
203 extern double stylusx, stylusy;
204 extern float styluspressure;
205 extern int stylusok;
206 extern int quitting;
208 char* LoadSaveDialog(bool save, bool levels, const char * title);