convert \r\n to \n
[hex-a-hop.git] / state.h
blob03a1c70983e068b40862ddf4f9afc645f989fb60
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 ~StateMakerBase() {}
155 virtual State* Create() = 0;
156 void Destroy()
158 delete state;
159 state = 0;
161 static State* GetNew(int k)
163 for (StateMakerBase* s = first; s; s=s->next)
164 if(k==s->key)
165 return current = s->Create();
166 return current;
168 static State* GetNew()
170 if (!first)
172 FATAL("StateMakerBase::GetNew - first is NULL");
173 return 0;
175 for (StateMakerBase* s = first; s; s=s->next)
176 if(s->start)
177 return current = s->Create();
178 return current = first->Create();
180 static void DestroyAll()
182 for (StateMakerBase* s = first; s; s=s->next)
183 s->Destroy();
184 current = 0;
188 template<class X>
189 class StateMaker : public StateMakerBase
191 public:
192 StateMaker(int key, bool start=false) : StateMakerBase(key, start)
194 State* Create()
196 if (!state) state = new X;
197 return state;
201 #define MAKE_STATE(x,key,start) static StateMaker<x> _maker_##x(key, start);
203 extern int mouse_buttons, mousex, mousey, noMouse;
204 extern double stylusx, stylusy;
205 extern float styluspressure;
206 extern int stylusok;
207 extern int quitting;
209 char* LoadSaveDialog(bool save, bool levels, const char * title);