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
25 // Uncomment this to check cross-platform compilation compatibility
28 //#define USE_BBTABLET
35 // End of config block
38 // Hacky workaround for MSVC's broken for scoping
39 #define for if (0) ; else for
43 #include <SDL/SDL_OpenGL.h>
51 extern SDL_Surface
* screen
;
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); }
58 static inline void FATAL(const char * string
="Unknown", const char * string2
="") { fprintf(stderr
, "Fatal error: %s \"%s\"\n", string
, string2
); exit(0); }
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
]='/'; }
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
99 virtual bool KeyPressed(int key, int mod)
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)
116 virtual void Render()
120 virtual void ScreenModeChanged()
125 MAKE_STATE(ClassName, _KEY_, false);
127 ************************************************************************/
131 static StateMakerBase
* first
;
132 StateMakerBase
* next
;
138 static State
* current
;
141 StateMakerBase(int key
, bool start
) : state(NULL
)
143 for (StateMakerBase
* s
= first
; s
; s
=s
->next
)
146 FATAL("Duplicate key in StateMakerBase::StateMakerBase");
154 virtual ~StateMakerBase() {}
155 virtual State
* Create() = 0;
161 static State
* GetNew(int k
)
163 for (StateMakerBase
* s
= first
; s
; s
=s
->next
)
165 return current
= s
->Create();
168 static State
* GetNew()
172 FATAL("StateMakerBase::GetNew - first is NULL");
175 for (StateMakerBase
* s
= first
; s
; s
=s
->next
)
177 return current
= s
->Create();
178 return current
= first
->Create();
180 static void DestroyAll()
182 for (StateMakerBase
* s
= first
; s
; s
=s
->next
)
189 class StateMaker
: public StateMakerBase
192 StateMaker(int key
, bool start
=false) : StateMakerBase(key
, start
)
196 if (!state
) state
= new X
;
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
;
209 char* LoadSaveDialog(bool save
, bool levels
, const char * title
);