summaryrefslogtreecommitdiff
path: root/apps/plugins/doom/i_video.c
blob: b77205ff8492a6e12c127e564a8e8fe441b87cbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* Emacs style mode select   -*- C++ -*-
 *-----------------------------------------------------------------------------
 *
 * $Id$
 *
 * Copyright (C) 1993-1996 by id Software, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * $Log$
 * Revision 1.1  2006/03/28 15:44:01  dave
 * Patch #2969 - Doom!  Currently only working on the H300.
 *
 *
 * DESCRIPTION:
 * DOOM graphics and buttons. H300 Port by Karl Kurbjun
 *      IPOD port by Dave Chapman and Paul Louden
 *      Additional work by Thom Johansen
 *
 *-----------------------------------------------------------------------------
 */

#include "doomstat.h"
#include "i_system.h"
#include "v_video.h"
#include "m_argv.h"
#include "d_main.h"

#include "doomdef.h"

#include "rockmacros.h"

static fb_data palette[256] IBSS_ATTR;
static fb_data *paldata=NULL;

#if !defined(CPU_COLDFIRE) || defined(SIMULATOR)
/*
   This code is credit to the IDOOM port.  It is not used for the H300, but
   serves as a good reference point for other targets.
*/
static fb_data * xtable  = 0;
static fb_data * ytable1 = 0;
static fb_data * ytable2 = 0;

#define FLOOR4(a) \
 (( a >> 2) << 2)

static int video_w, video_h;

static void genscalexytable(void)
{
   // shall we use uint8_t intead of ints?
   int y = video_h-1;
   int x = video_w-1;
   int i = 1 + (x>y?x:y);
   xtable  = malloc(sizeof(int)*video_w );
   ytable1 = malloc(sizeof(int)*video_h );
   ytable2 = malloc(sizeof(int)*video_h );

   while(i--)
   {
      if(y>=0)
      {
         ytable1[y] = ((y*SCREENHEIGHT) / video_h) * SCREENWIDTH;
         ytable2[y] = y*video_w;
         y--;
      }
      if(x>=0)
      {
         xtable[x] = (x*SCREENWIDTH) / video_w;
         x--;
      }
   }
}
#endif

//
// I_ShutdownGraphics (NOT USED)
//
void I_ShutdownGraphics(void)
{
}

//
// I_StartFrame (NOT USED)
//
void I_StartFrame (void)
{
}

//
// I_GetEvent (NOT USED)
//
void I_GetEvent(void)
{
}

//
// I_StartTic
//

#if CONFIG_KEYPAD == IPOD_4G_PAD
//#define DOOMBUTTON_SCROLLWHEEL
#define DOOMBUTTON_UP         BUTTON_MENU
#define DOOMBUTTON_WEAPON     BUTTON_SELECT
#define DOOMBUTTON_LEFT       BUTTON_LEFT
#define DOOMBUTTON_RIGHT      BUTTON_RIGHT
#define DOOMBUTTON_SHOOT      BUTTON_PLAY
#define DOOMBUTTON_ENTER      BUTTON_SELECT
#define DOOMBUTTON_OPEN       BUTTON_MENU
#else
#define DOOMBUTTON_UP      BUTTON_UP
#define DOOMBUTTON_DOWN    BUTTON_DOWN
#define DOOMBUTTON_LEFT    BUTTON_LEFT
#define DOOMBUTTON_RIGHT   BUTTON_RIGHT
#define DOOMBUTTON_SHOOT   BUTTON_REC
#define DOOMBUTTON_OPEN    BUTTON_MODE
#define DOOMBUTTON_ESC     BUTTON_OFF
#define DOOMBUTTON_ENTER   BUTTON_SELECT
#define DOOMBUTTON_WEAPON  BUTTON_ON
#endif

int getkey(event_t * event)
{
   // Same button handling as rockboy
   static unsigned int oldbuttonstate = 0, newbuttonstate=0;

   static int released, pressed;

#if CONFIG_KEYPAD == IRIVER_H300_PAD
   static unsigned int holdbutton=0;
   static int hswitch=0;
   if (rb->button_hold()&~holdbutton)
   {
      if(hswitch==0)
      {
         event->type = ev_keydown;
         hswitch=1;
      }
      else
      {
         event->type = ev_keyup;
         hswitch=0;
      }
      event->data1=KEY_RSHIFT;
      D_PostEvent(event);
   }
   holdbutton=rb->button_hold();
#endif

   newbuttonstate = rb->button_status();
   released = ~newbuttonstate & oldbuttonstate;
   pressed = newbuttonstate & ~oldbuttonstate;
   oldbuttonstate = newbuttonstate;
   if(released)
   {
      event->type = ev_keyup;
      if(released & DOOMBUTTON_LEFT)
      {
         event->data1=KEY_LEFTARROW;
         D_PostEvent(event);
      }
      if(released & DOOMBUTTON_RIGHT)
      {
         event->data1=KEY_RIGHTARROW;
         D_PostEvent(event);
      }
#ifdef DOOMBUTTON_DOWN
      if(released & DOOMBUTTON_DOWN)
      {
         event->data1=KEY_DOWNARROW;
         D_PostEvent(event);
      }
#endif
      if(released & DOOMBUTTON_UP)
      {
         event->data1=KEY_UPARROW;
         D_PostEvent(event);
      }
      if(released & DOOMBUTTON_SHOOT)
      {
         event->data1=KEY_RCTRL;
         D_PostEvent(event);
      }
      if(released & DOOMBUTTON_OPEN)
      {
         event->data1=' ';
         D_PostEvent(event);
      }
#ifdef DOOMBUTTON_ESC
      if(released & DOOMBUTTON_ESC)
      {
         event->data1=KEY_ESCAPE;
         D_PostEvent(event);
      }
#endif
#ifdef DOOMBUTTON_ENTER
      if(released & DOOMBUTTON_ENTER)
      {
         event->data1=KEY_ENTER;
         D_PostEvent(event);
      }
#endif
#ifdef DOOMBUTTON_WEAPON
      if(released & DOOMBUTTON_WEAPON)
      {
         event->data1 ='w';
         D_PostEvent(event);
      }
#endif
   }
   if(pressed)
   {
      event->type = ev_keydown;
      if(pressed & DOOMBUTTON_LEFT)
      {
         event->data1=KEY_LEFTARROW;
         D_PostEvent(event);
      }
      if(pressed & DOOMBUTTON_RIGHT)
      {
         event->data1=KEY_RIGHTARROW;
         D_PostEvent(event);
      }
#ifdef DOOMBUTTON_DOWN
      if(pressed & DOOMBUTTON_DOWN)
      {
         event->data1=KEY_DOWNARROW;
         D_PostEvent(event);
      }
#endif
      if(pressed & DOOMBUTTON_UP)
      {
         event->data1=KEY_UPARROW;
         D_PostEvent(event);
      }
      if(pressed & DOOMBUTTON_SHOOT)
      {
         event->data1=KEY_RCTRL;
         D_PostEvent(event);
      }
      if(pressed & DOOMBUTTON_OPEN)
      {
         event->data1=' ';
         D_PostEvent(event);
      }
#ifdef DOOMBUTTON_ESC
      if(pressed & DOOMBUTTON_ESC)
      {
         event->data1=KEY_ESCAPE;
         D_PostEvent(event);
      }
#endif
#ifdef DOOMBUTTON_ENTER
      if(pressed & DOOMBUTTON_ENTER)
      {
         event->data1=KEY_ENTER;
         D_PostEvent(event);
      }
#endif
#ifdef DOOMBUTTON_WEAPON
      if(pressed & DOOMBUTTON_WEAPON)
      {
         event->data1='w';
         D_PostEvent(event);
      }
#endif
   }
   if(pressed || released)
      return 1;
   else
      return 0;
}

event_t event;
void I_StartTic (void)
{
   getkey(&event);
}


///////////////////////////////////////////////////////////
// Palette stuff.
//
static void I_UploadNewPalette(int pal)
{
  // This is used to replace the current 256 colour cmap with a new one
  // Used by 256 colour PseudoColor modes
  static int cachedgamma;
  static size_t num_pals;

  if ((paldata == NULL) || (cachedgamma != usegamma)) {
    int lump = W_GetNumForName("PLAYPAL");
    const byte *pall = W_CacheLumpNum(lump);
    register const byte *const gtable = gammatable[cachedgamma = usegamma];
    register int i;

    num_pals = W_LumpLength(lump) / (3*256);
    num_pals *= 256;

    if (!paldata) {
      // First call - allocate and prepare colour array
      paldata = malloc(sizeof(*paldata)*num_pals);
    }

    // set the colormap entries
    for (i=0 ; (size_t)i<num_pals ; i++) {
      int r = gtable[pall[0]];
      int g = gtable[pall[1]];
      int b = gtable[pall[2]];
      pall+=3;
      paldata[i] = LCD_RGBPACK(r,g,b);
    }

    W_UnlockLumpNum(lump);
    num_pals/=256;
  }

#ifdef RANGECHECK
  if ((size_t)pal >= num_pals)
    I_Error("I_UploadNewPalette: Palette number out of range (%d>=%d)",
      pal, num_pals);
#endif
   memcpy(palette,paldata+256*pal,256*sizeof(fb_data));
}

//
// I_UpdateNoBlit
//
void I_UpdateNoBlit (void)
{
}

//
// I_FinishUpdate
//

void I_FinishUpdate (void)
{
#if defined(CPU_COLDFIRE) && !defined(SIMULATOR)
   /*
      Faster screen update than the lookuptables -> I'm wasting 7 pixels of width
      though.  This code also doesn't use the framebuffer so rockbox's drawing
      functions will not work on top of the doom drawing.
   */

   // Start the write
   *(volatile unsigned short *) 0xf0000000 = 0x21; // register
   *(volatile unsigned short *) 0xf0000002 = 0;    // value
   *(volatile unsigned short *) 0xf0000000 = 0x22; // GRAM

   unsigned char *screenptr=screens[0];
   int wcnt=0, hcnt=0;

   *(volatile unsigned short *) 0xf0000002 = 0;
   *(volatile unsigned short *) 0xf0000002 = 0;
   *(volatile unsigned short *) 0xf0000002 = 0;

   while(hcnt<LCD_HEIGHT)
   {
      while(wcnt<LCD_WIDTH-7)
      {
         if((wcnt&0x01))
            screenptr++; // Skip every so many pixels in Doom buffer
         *(volatile unsigned short *)0xf0000002 = palette[*screenptr];
         screenptr++;
         wcnt++;
      }
      screenptr++;
      // move on past those 7 pixels
      *(volatile unsigned short *) 0xf0000002 = 0;
      *(volatile unsigned short *) 0xf0000002 = 0;
      *(volatile unsigned short *) 0xf0000002 = 0;
      *(volatile unsigned short *) 0xf0000002 = 0;
      *(volatile unsigned short *) 0xf0000002 = 0;
      *(volatile unsigned short *) 0xf0000002 = 0;
      *(volatile unsigned short *) 0xf0000002 = 0;
      wcnt=0;
      hcnt++;
      if((hcnt&0x07)==0x07)
         screenptr+=SCREENWIDTH; // Skip every 7th line
   }
#else
   // The IDOOM code for screen updates
   unsigned char paletteIndex;
   int x, y;

   for (y = 0; y < video_h; y++)
   {
      for (x = 0; x < video_w; x++)
      {
#if LCD_HEIGHT >= SCREENHEIGHT
         paletteIndex = screens[0][((y*SCREENHEIGHT) / video_h) 
                                   * SCREENWIDTH + x];
         rb->lcd_framebuffer[y * video_w + x] = palette[paletteIndex];
#else
         paletteIndex = screens[0][ytable1[y] +xtable[x]];
         rb->lcd_framebuffer[x + ytable2[y]] = palette[paletteIndex];
#endif
      }
   }
   rb->lcd_update();
#endif
}

//
// I_ReadScreen
//
void I_ReadScreen (byte* scr)
{
   memcpy (scr, screens[0], SCREENWIDTH*SCREENHEIGHT);
}

//
// I_SetPalette
//
void I_SetPalette (int pal)
{
   I_UploadNewPalette(pal);
}

//
// I_InitGraphics
//
void I_InitGraphics(void)
{
   static int  firsttime=1;

   if (!firsttime)
      return;
   firsttime = 0;

   printf("Starting Graphics engine\n");

   /* Note: The other screens are initialized later */
   screens[0] = malloc (SCREENWIDTH * SCREENHEIGHT * sizeof(unsigned char));

#if defined(CPU_COLDFIRE) && !defined(SIMULATOR)
   coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
#else

   video_h = LCD_HEIGHT;
   video_w = FLOOR4(LCD_WIDTH); // From IDOOM, the width has to be a multiple of 4
   genscalexytable();
#endif
}