~ubuntu-branches/ubuntu/karmic/gravitywars/karmic

« back to all changes in this revision

Viewing changes to vgastubs.c

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hocevar (Debian packages)
  • Date: 2003-06-12 13:31:00 UTC
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20030612133100-unu8ek7z4109neyb
Tags: 1.102-27
* debian/control:
  + Build-depend on libsdl1.2-dev (>= 1.2.5-8) because 1.2.5-7 had a broken
    shlibs file.
  + Removed the build-dependency on dpkg-dev since it is in build-essential.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/* GravityWars 1.1, SDL wrapper (C) 2003 Sam Hocevar <sam@zoy.org> */
 
3
 
 
4
#include <stdio.h>
 
5
#include <stdlib.h>
 
6
#include <string.h>
 
7
 
 
8
#include "memory.h"
 
9
 
 
10
#ifdef USE_SDL
 
11
#include "vgastubs.h"
 
12
 
 
13
#include <SDL.h>
 
14
 
 
15
/* VGA */
 
16
 
 
17
#define WIDTH 640
 
18
#define HEIGHT 480
 
19
 
 
20
SDL_Surface *screen;
 
21
SDL_Surface *bg;
 
22
SDL_Rect score_rect, level_rect, game_rect;
 
23
 
 
24
int vga_init(void) {
 
25
  if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) < 0)
 
26
    return -1;
 
27
 
 
28
  atexit(SDL_Quit);
 
29
 
 
30
  return 0;
 
31
}
 
32
 
 
33
int vga_setmode(int mode) {
 
34
 
 
35
  switch(mode) {
 
36
    case G640x480x256:
 
37
      screen = SDL_SetVideoMode(WIDTH, HEIGHT, 8,
 
38
                                SDL_HWPALETTE/*|SDL_FULLSCREEN*/);
 
39
      if(!screen) {
 
40
        fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
 
41
                WIDTH, HEIGHT, SDL_GetError());
 
42
        return -1;
 
43
      }
 
44
      break;
 
45
    case TEXT:
 
46
      break;
 
47
  }
 
48
 
 
49
  /* The level area in the background bitmap */
 
50
  level_rect.w = WIDTH;
 
51
  level_rect.h = HEIGHT;
 
52
  level_rect.x = 0;
 
53
  level_rect.y = 16; /* gets modified, between 16 and 960 */
 
54
 
 
55
  /* Where our viewport gets blitted */
 
56
  game_rect.w = WIDTH;
 
57
  game_rect.h = HEIGHT;
 
58
  game_rect.x = 0;
 
59
  game_rect.y = 0;
 
60
 
 
61
  /* The score area in the background bitmap */
 
62
  score_rect.w = WIDTH;
 
63
  score_rect.h = 0;
 
64
  score_rect.x = 0;
 
65
  score_rect.y = 16;
 
66
 
 
67
  /* Where the score gets blitted - heh, we use NULL! */
 
68
 
 
69
  return 0;
 
70
}
 
71
 
 
72
/* This one is SDL-only */
 
73
void vga_setsplitline(int sl) {
 
74
  score_rect.h = 480 - sl;
 
75
  score_rect.y = 16 - (480 - sl);
 
76
}
 
77
 
 
78
#ifndef MIN
 
79
#define MIN(a, b) ((a) < (b) ? (a) : (b))
 
80
#endif
 
81
#ifndef MAX
 
82
#define MAX(a, b) ((a) > (b) ? (a) : (b))
 
83
#endif
 
84
 
 
85
int vga_claimvideomemory(int m) {
 
86
  bg = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, m / WIDTH, 8, 0, 0, 0, 0);
 
87
 
 
88
  if(!bg)
 
89
    return -1;
 
90
 
 
91
  /* set the palette to the logical screen palette so that blits
 
92
     won't be translated */
 
93
  SDL_SetColors(bg, screen->format->palette->colors, 0, 256);
 
94
 
 
95
  vga_setpage(0);
 
96
 
 
97
  return 0;
 
98
}
 
99
 
 
100
unsigned char *vga_getgraphmem(void) {
 
101
  return bg->pixels;
 
102
}
 
103
 
 
104
/* FIXME: implement this as a macro */
 
105
void vga_setpage(int page) {
 
106
  vga_ptr = bg->pixels + (page << 16);
 
107
}
 
108
 
 
109
void vga_waitretrace(void) {
 
110
  SDL_BlitSurface(bg, &level_rect, screen, &game_rect);
 
111
  SDL_BlitSurface(bg, &score_rect, screen, NULL);
 
112
  SDL_UpdateRect(screen, 0, 0, 0, 0);
 
113
}
 
114
 
 
115
/* gl */
 
116
 
 
117
void gl_setpalette(void *sp) {
 
118
  SDL_Color cmap[256];
 
119
  char *cols = (char *)sp;
 
120
  int i;
 
121
 
 
122
  for(i=0; i<256; i++) {
 
123
    cmap[i].r = *cols++ * 4;
 
124
    cmap[i].g = *cols++ * 4;
 
125
    cmap[i].b = *cols++ * 4;
 
126
  }
 
127
 
 
128
  SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, 256);
 
129
}
 
130
 
 
131
int gl_setcontextvga(int mode) {
 
132
  return 0;
 
133
}
 
134
 
 
135
void  gl_setcontextvirtual(int  w,  int  h,  int  bpp, int bitspp, void *vbuf) {
 
136
  return;
 
137
}
 
138
 
 
139
void *gl_setdisplaystart(int x, int y) {
 
140
  level_rect.x = x;
 
141
  level_rect.y = y; /* y >= 16 */
 
142
 
 
143
  return NULL;
 
144
}
 
145
 
 
146
void gl_setpalettecolor(int c, int r, int g, int b) {
 
147
  SDL_Color cmap[1];
 
148
  cmap[0].r = r * 4;
 
149
  cmap[0].g = g * 4;
 
150
  cmap[0].b = b * 4;
 
151
 
 
152
  SDL_SetColors(screen, cmap, 0, 1);
 
153
}
 
154
 
 
155
/* Mouse */
 
156
 
 
157
int mouse_init(char *dev, int type, int samplerate) {
 
158
  return 0;
 
159
}
 
160
 
 
161
int mouse_update(void) {
 
162
  return 0;
 
163
}
 
164
 
 
165
int mouse_getbutton(void) {
 
166
  return 0;
 
167
}
 
168
 
 
169
void mouse_close(void) {
 
170
  return;
 
171
}
 
172
 
 
173
/* Keyboard */
 
174
 
 
175
unsigned char keybuf[256];
 
176
 
 
177
int keyboard_init(void) {
 
178
  scans = keybuf;
 
179
  memset(scans, 0, 256);
 
180
  return 0;
 
181
}
 
182
 
 
183
void keyboard_close(void) {
 
184
  return;
 
185
}
 
186
 
 
187
char *keyboard_getstate(void) {
 
188
  return scans;
 
189
}
 
190
 
 
191
int keyboard_update(void) {
 
192
  SDL_Event e;
 
193
  int updated = 0;
 
194
  int mod;
 
195
 
 
196
  mod = SDL_GetModState();
 
197
  scans[SCANCODE_LEFTCONTROL] = mod & KMOD_LCTRL;
 
198
  scans[SCANCODE_RIGHTCONTROL] = mod & KMOD_RCTRL;
 
199
 
 
200
  while(SDL_PollEvent(&e)) {
 
201
    if(e.type == SDL_KEYDOWN) {
 
202
      switch(e.key.keysym.sym) {
 
203
        case SDLK_ESCAPE:
 
204
          scans[SCANCODE_ESCAPE] = 1;
 
205
          updated = 1;
 
206
          break;
 
207
        case SDLK_LEFT:
 
208
          scans[SCANCODE_CURSORBLOCKLEFT] = 1;
 
209
          updated = 1;
 
210
          break;
 
211
        case SDLK_RIGHT:
 
212
          scans[SCANCODE_CURSORBLOCKRIGHT] = 1;
 
213
          updated = 1;
 
214
          break;
 
215
        case SDLK_UP:
 
216
          scans[SCANCODE_CURSORBLOCKUP] = 1;
 
217
          updated = 1;
 
218
          break;
 
219
        case SDLK_f:
 
220
          SDL_WM_ToggleFullScreen(screen);
 
221
          break;
 
222
        default:
 
223
          updated = 1;
 
224
          break;
 
225
      }
 
226
    } else if(e.type == SDL_KEYUP) {
 
227
      switch(e.key.keysym.sym) {
 
228
        case SDLK_ESCAPE:
 
229
          scans[SCANCODE_ESCAPE] = 0;
 
230
          break;
 
231
        case SDLK_LEFT:
 
232
          scans[SCANCODE_CURSORBLOCKLEFT] = 0;
 
233
          break;
 
234
        case SDLK_RIGHT:
 
235
          scans[SCANCODE_CURSORBLOCKRIGHT] = 0;
 
236
          break;
 
237
        case SDLK_UP:
 
238
          scans[SCANCODE_CURSORBLOCKUP] = 0;
 
239
          break;
 
240
        default:
 
241
          break;
 
242
      }
 
243
    }
 
244
  }
 
245
 
 
246
  return updated;
 
247
}
 
248
 
 
249
#endif /* USE_SDL */