forked from bentorkington/sf2ww
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OGLView.m
318 lines (254 loc) · 8.39 KB
/
OGLView.m
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
//
// OGLView.m
// Points
//
// Created by Ben on 26/05/11.
// Copyright 2011 Ben Torkington. All rights reserved.
//
#import "OGLView.h"
#import "GLUT/glut.h"
#import "trackball.h"
#include "sf2const.h"
#include "sf2types.h"
#include "sf2macros.h"
#include "gstate.h"
#include "structs.h"
#include "particle.h"
#include "player.h"
#include "sm.h"
#include "gfx_glut.h"
//#include "lib.h"
#include "gemu.h"
#include "glwimp.h"
#include "workarounds.h"
#include "pthreads.h"
#include "sf2io.h"
#include "game.h"
#include "task.h"
#include "redhammer.h"
#define DEBUG TRUE
extern Game g;
extern CPSGFXEMU gemu;
extern int gGameInWindow;
LBView *gGameTitleBar;
LBView *gGameWindowIcon;
LBView *gGameArea;
extern LBView *gameView;
extern LBWindow *dummyWindow;
extern LBView rootView;
void mouse (int button, int state, int x, int y);
void drawGLString(GLfloat x, GLfloat y, char *string);
void GameViewClicked(LBView *view, int button, int state, int x, int y);
void renderDummy(struct view *);
int gtimercount = 0;
int gsupertaskcnt = 0;
int gdrawallcnt = 0;
extern struct inputs gInputs;
GLfloat gShapeSize = 11.0f;
@implementation OGLView
- (void)prepare
{
NSLog(@"prepare");
NSOpenGLContext *glcontext = [self openGLContext];
[glcontext makeCurrentContext];
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
//glEnable(GL_DEPTH_TEST);
glDisable(GL_DEPTH_TEST);
GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glEnable(GL_LIGHT0);
GLfloat mata[] = {0.1, 0.1, 0.1, 1.0};
GLfloat matb[] = {0.9, 0.9, 0.9, 1.0};
glMaterialfv(GL_FRONT, GL_AMBIENT, mata);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matb);
gCameraReset();
//glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glFrontFace(GL_CCW);
glColor3f(1.0,1.0,1.0);
glClearColor(0.0, 0.0, 0.0, 1.0);
glPolygonOffset (1.0, 1.0);
glEnable(GL_LIGHTING);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_SMOOTH);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_SMOOTH);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gfx_glut_init();
load_cps_roms();
glwimp_init(900, 600);
dummyWindow = createWindow(40, 50, 384, 224);
dummyWindow->areaView->renderFunc = &renderDummy;
gameView = addSubView(&rootView, &DrawNull, &ClickNull, 10, 10, 400, 300 + WIN_TITLE_HEIGHT);
gGameWindowIcon = addSubView(gameView, &DrawWindowButton, &WindowButtonClicked, 2, 2, WIN_TITLE_HEIGHT - 4, WIN_TITLE_HEIGHT - 4);
gGameTitleBar = addSubView(gameView, &DrawTitleBar, &DragBarClicked, WIN_TITLE_HEIGHT + 8, 2, 400 - (WIN_TITLE_HEIGHT + 10), WIN_TITLE_HEIGHT - 4);
gGameArea = addSubView(gameView, &DrawNull, &GameViewClicked, 0, WIN_TITLE_HEIGHT, 400, 300); // XXX
manual_init();
timer = [[NSTimer scheduledTimerWithTimeInterval:1.0 / 60
target:self
selector:@selector(timerCallback)
userInfo:nil
repeats:YES] retain];
}
-(IBAction)resetGame:(id)sender
{
NSLog(@"todo");
}
void renderDummy(struct view *view) {
glPushMatrix();
glEnable(GL_TEXTURE_2D);
gfx_glut_drawgame();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
- (id)initWithCoder:(NSCoder *)c
{
self = [super initWithCoder:c];
[self prepare];
return self;
}
- (void)reshape
{
baseRect = [self convertRectToBase:[self bounds]];
gfx_glut_reshape(baseRect.size.width, baseRect.size.height);
[super reshape];
}
- (void)awakeFromNib
{
[self setNeedsDisplay:YES];
}
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)timerCallback
{
task_timer();
[self setNeedsDisplay:YES];
}
- (void)drawGame:(NSRect)dirtyRect {
gfx_glut_drawgame();
}
- (void)drawRect:(NSRect)r {
[self drawGame:r];
glEnable(GL_LIGHTING);
glColor3f(0.5, 0.5, 0.5);
glFinish();
}
- (NSPoint)convertToOpenGL:(NSPoint)p
{
NSPoint q;
q.y = baseRect.size.height - p.y;
q.x = p.x;
return q;
}
- (void)mouseDown:(NSEvent *)event
{
NSPoint p = [event locationInWindow];
NSPoint q = [self convertToOpenGL:p];
gfx_glut_mousedown(q.x, q.y);
}
- (void)rightMouseDown:(NSEvent *)event
{
NSPoint p = [self convertToOpenGL:[event locationInWindow]];
gfx_glut_rightmousedown(p.x, p.y);
}
- (void)rightMouseDragged:(NSEvent *)event
{
NSPoint p = [self convertToOpenGL:[event locationInWindow]];
gfx_glut_rightmousedragged(p.x, p.y);
[self setNeedsDisplay:YES];
}
- (void)mouseDragged:(NSEvent *)event
{
NSPoint p = [self convertToOpenGL:[event locationInWindow]];
gfx_glut_mousedragged(p.x, p.y);
[self setNeedsDisplay:YES];
}
- (void)mouseUp:(NSEvent *)event
{
NSPoint p = [self convertToOpenGL:[event locationInWindow]];
gfx_glut_mouseup(p.x, p.y);
}
- (void)rightMouseUp:(NSEvent *)event
{
NSPoint p = [self convertToOpenGL: [event locationInWindow]];
gfx_glut_rightmouseup(p.x, p.y);
}
- (BOOL)acceptsFirstResponder
{
return YES; // YES, yes I do.
}
- (BOOL)resignFirstResponder
{
[self setNeedsDisplay:YES];
return YES;
}
- (BOOL)becomeFirstResponder
{
[self setNeedsDisplay:YES];
return YES;
}
- (void)keyDown:(NSEvent *)theEvent
{
unichar keypress;
if ([theEvent isARepeat] == NO) {
keypress = [[theEvent characters]characterAtIndex:0];
switch (keypress) {
case NSUpArrowFunctionKey: gInputs.p10 |= JOY_UP; break;
case NSDownArrowFunctionKey: gInputs.p10 |= JOY_DOWN; break;
case NSLeftArrowFunctionKey: gInputs.p10 |= JOY_LEFT; break;
case NSRightArrowFunctionKey: gInputs.p10 |= JOY_RIGHT; break;
case 'q': gInputs.p10 |= BUTTON_A; break;
case 'w': gInputs.p10 |= BUTTON_B; break;
case 'e': gInputs.p10 |= BUTTON_C; break;
case 'a': gInputs.p11 |= BUTTON_D >> 8; break;
case 's': gInputs.p11 |= BUTTON_E >> 8; break;
case 'd': gInputs.p11 |= BUTTON_F >> 8; break;
case '1': gInputs.in0 |= IPT_START1; break;
case '2': gInputs.in0 |= IPT_START2; break;
case '5': gInputs.in0 |= IPT_COIN1; break;
case '6': gInputs.in0 |= IPT_COIN2; break;
case '7': gemu_flip_scroll_enable(0); break;
case '8': gemu_flip_scroll_enable(1); break;
case '9': gemu_flip_scroll_enable(2); break;
case '0': gemu_flip_scroll_enable(3); break;
case 'T': print_task_table(); break;
case 'K': g.Player2.Energy = -1; break;
case 'p': g.JPParam ^= JP_FREEZE; break;
case 'W': gGameInWindow = 1-gGameInWindow; break;
default:
break;
}
}
}
-(void)keyUp:(NSEvent *)theEvent
{
unichar keypress = [[theEvent characters]characterAtIndex:0];
switch (keypress) {
case 'q': gInputs.p10 &= ~(BUTTON_A); break;
case 'w': gInputs.p10 &= ~(BUTTON_B); break;
case 'e': gInputs.p10 &= ~(BUTTON_C); break;
case 'a': gInputs.p11 &= ~(BUTTON_D >> 8); break;
case 's': gInputs.p11 &= ~(BUTTON_E >> 8); break;
case 'd': gInputs.p11 &= ~(BUTTON_F >> 8); break;
case '1': gInputs.in0 &= ~IPT_START1; break;
case '2': gInputs.in0 &= ~IPT_START2; break;
case '5': gInputs.in0 &= ~IPT_COIN1; break;
case '6': gInputs.in0 &= ~IPT_COIN2; break;
case NSUpArrowFunctionKey: gInputs.p10 &= ~JOY_UP; break;
case NSDownArrowFunctionKey: gInputs.p10 &= ~JOY_DOWN; break;
case NSLeftArrowFunctionKey: gInputs.p10 &= ~JOY_LEFT; break;
case NSRightArrowFunctionKey: gInputs.p10 &= ~JOY_RIGHT; break;
default:
break;
}
}
void GameViewClicked(LBView *view, int button, int state, int x, int y) {
}
@end