source: golgotha/src/i4/main/win_main.cc @ 608

Last change on this file since 608 was 80, checked in by Sam Hocevar, 15 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 4.9 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "main/main.hh"
10#include "main/win_main.hh"
11#include "error/error.hh"
12#include "error/alert.hh"
13#include "file/file.hh"
14
15// this global will be used by video/directx/directx.c when opening a window
16#include <windows.h>
17#include <windowsx.h>
18#include <stdio.h>
19
20w32           i4_global_argc;
21i4_const_str *i4_global_argv;
22
23
24HINSTANCE i4_win32_instance;
25int       i4_win32_nCmdShow;
26HWND      i4_win32_window_handle=0;
27i4_win32_startup_options_struct i4_win32_startup_options;
28void *i4_stack_base=0;
29
30int i4_win32_alert(const i4_const_str &ret)
31{
32  char tmp[1024], *st;
33  strcpy(tmp, "Alert : ");
34  st=tmp+8;
35
36  i4_const_str::iterator s=ret.begin();
37  while (s!=ret.end())
38  {
39    *st=s.get().value();
40    ++s;
41    st++;
42  }
43  *st=0;
44  strcat(tmp,"\n");
45  OutputDebugString(tmp);
46  setbuf(stdout,0);
47  printf(tmp);
48  return 1;
49}
50
51
52void i4_win32_startup_options_struct::check_option(char *opt)
53{
54  if (!strcmp(opt,"-no_full"))
55    fullscreen=i4_F;
56}
57
58
59int i4_win32_error(const char *st)
60{
61
62  static int died = 0;
63
64  if (died)
65  {
66    i4_debug->printf("recursive error : %s", st);
67    return 1; 
68  }
69
70  died = 1;
71
72  MessageBox(0, st, "Error!", MB_OK | MB_APPLMODAL);
73 
74  exit(0);
75
76  return 1;
77}
78
79
80
81static FILE *debug_file=0;
82
83
84int i4_windows_warning_function(const char *st)
85{
86  OutputDebugString(st);
87  OutputDebugString("\n");
88
89  return 1;
90}
91
92class i4_win32_debug_stream_class : public i4_file_class
93{
94public:
95  virtual w32 read (void *buffer, w32 size) { return 0; }
96
97  virtual w32 write(const void *buffer, w32 size)
98  {
99    if (debug_file)
100    {
101      fwrite(buffer, 1, size, debug_file);
102      fflush(debug_file);
103    }
104
105    char b[1024];
106    if (size>1023)
107    {
108      b[1]=0;
109      for (int i=0; i<size; i++)
110      {
111        b[0]=*(((w8 *)buffer)+i);
112        OutputDebugString(b);
113      }
114    }
115    else
116    {
117      memcpy(b,buffer, size);
118      b[size]=0;
119      OutputDebugString(b);
120    }
121    return size;
122  }
123
124  virtual w32 seek (w32 offset) { return 0; }
125  virtual w32 size () { return 0; }
126  virtual w32 tell () { return 0; }
127} win32_debug_stream;
128
129
130
131void debug_init()
132{
133  i4_set_error_function(i4_win32_error);
134  i4_set_warning_function(i4_windows_warning_function);
135  i4_set_alert_function(i4_win32_alert);
136
137  for (int i=1; i<i4_global_argc; i++)
138  {
139    if (i4_global_argv[i]=="-eout")
140    {
141      i++;
142      char fn[100];
143      i4_os_string(i4_global_argv[i], fn, 100);
144      debug_file=fopen(fn, "wb");
145    }
146  }
147
148  i4_debug=&win32_debug_stream;
149}
150
151
152
153class tmp_main_str : public i4_const_str
154{
155public:
156  tmp_main_str();
157  void set_ptr(char *_ptr)
158  {
159    ptr=_ptr;
160    len=strlen(ptr);
161  }
162
163};
164
165
166
167int main(int argc, char **argv)
168{
169  long t;
170  i4_stack_base=(void *)(&t);
171
172  tmp_main_str *tmp=(tmp_main_str *)malloc(sizeof(tmp_main_str)*(argc+1));
173  w32 i;
174  for (i=0; i<argc; i++)
175    tmp[i].set_ptr(argv[i]);
176
177  i4_global_argc=argc;
178  i4_global_argv=tmp;
179  debug_init();
180  i4_main(argc,tmp);
181
182  free(tmp);
183 
184  return 1;
185}
186
187int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
188    LPSTR lpCmdLine, int nCmdShow)
189
190
191{
192  long t;
193  i4_stack_base=(void *)(&t);
194
195
196  /*
197  char hacked_args[512];
198  strcpy(hacked_args,"x:\\jc\\gdata\\obj\\test_text.gmod -no_full");
199  */
200  w32 count, white;
201  char *s,*p,*st = strdup(lpCmdLine);
202
203  s = st;
204  count = 1;
205  white = 1;
206  while (*s)
207  {
208    if (white)
209    {
210      if (*s != ' ')
211      {
212        count++;
213        white = 0;
214      }
215      s++;
216    }
217    else
218    {
219      if (*s == ' ')
220        white = 1;
221      s++;
222    }
223  }
224
225  tmp_main_str *tmp=(tmp_main_str *)malloc(sizeof(tmp_main_str)*(count+1));
226
227  s = st;
228  tmp[0].set_ptr("name");
229  count = 1;
230  white = 1;
231  while (*s)
232  {
233    if (white)
234    {
235      if (*s != ' ')
236      {
237        white = 0;
238        p = s;
239      }
240      s++;
241    }
242    else
243    {
244      if (*s == ' ')
245      {
246        white = 1;
247        *s = 0;
248
249        i4_win32_startup_options.check_option(p);
250         
251        tmp[count].set_ptr(p);
252        count++;
253      }
254      s++;
255    }
256  }
257  if (!white)
258  {
259    i4_win32_startup_options.check_option(p);
260    tmp[count].set_ptr(p);
261    count++;
262  }
263
264  i4_win32_nCmdShow=nCmdShow;
265  i4_win32_instance=hInstance;
266
267  i4_global_argc=count;
268  i4_global_argv=tmp;
269  debug_init();
270  i4_main(count, tmp); 
271  free(tmp);
272
273  PostQuitMessage( 0 );
274
275  return 0;
276}
Note: See TracBrowser for help on using the repository browser.