-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNDSA.hh
197 lines (161 loc) · 4.83 KB
/
NDSA.hh
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
#pragma once
#define SCREEN(x,y) "\x1b["#y";"#x"H"
#define PrintAt(x,y,str,...) printf(SCREEN(x,y) str, ##__VA_ARGS__)
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstdarg>
#include <cstring>
#include <typeinfo>
#include <NDSA/List.hh>
#include <NDSA/Collision.hh>
#ifdef NDSA_AUDIO
#include <maxmod9.h>
#include "mmsolution.h" // solution definitions
#include "mmsolution_bin.h" // solution binary reference
#include <NDSA/Audio.hh>
#endif
// these names are too long...
typedef const unsigned char TileData;
typedef const unsigned short MapData;
typedef const unsigned short PaletteData;
#define includeMap(x) \
extern const short unsigned int x ## Map[]; \
extern const unsigned long x ## MapLen;
#define includeTiles(x) \
extern const unsigned int x ## TilesLen; \
extern const unsigned int x ## PalLen; \
extern const unsigned char x ## Tiles[]; \
extern const unsigned short x ## Pal[];
#define TileData(x) x ## Tiles, x ## TilesLen, x ## Pal, x ## PalLen
#define MapData(x) x ## Map, x ## MapLen
namespace NDSA {
inline void Fatal(const char*,...);
}
#ifdef DS
#include <nds.h>
#define SCREEN_WIDTH 256
#define SCREEN_HEIGHT 192
#endif
#ifdef GBA
#include <gba.h>
#include <NDSA/GBA.hh>
#endif
namespace NDSA {
inline void initConsole() {
consoleDemoInit();
#ifdef GBA
// black background
BG_PALETTE[0] = 0;
BGCTRL[0] = BG_16_COLOR | SCREEN_BASE(4);
BG_OFFSET[0].x = 0; BG_OFFSET[0].y = 0;
#endif
}
inline void Fatal(const char *s,...) {
initConsole();
va_list argptr;
va_start(argptr,s);
fprintf(stdout, SCREEN(0,9) "ERROR: ");
vfprintf(stdout, s, argptr);
va_end(argptr);
while(1);
}
class Object;
extern PointerList<Object> Objects;
extern PointerList<Object> Colliders;
template<typename P, typename T>
inline void onCollision(void function(P*, T*)) {
P *p = new P();
T *t = new T();
CollisionEvent *c = new CollisionEvent(typeid(*p).name(), typeid(*t).name(), (void*)function);
CollisionEvents.add(c);
Objects.deleteByInstance<P>(&p);
Objects.deleteByInstance<T>(&t);
}
}
#ifdef DS
namespace NDSA {
enum NDSA_Screen {
TopScreen, BottomScreen
};
}
#endif
#include <NDSA/Input.hh>
#include <NDSA/Random.hh>
#include <NDSA/Background.hh>
#include <NDSA/Sprite.hh>
#include <NDSA/Object.hh>
namespace NDSA {
extern void Game();
extern struct SystemObj {
void Initialize() {
#ifdef DS
videoSetMode(MODE_0_2D);
videoSetModeSub(MODE_0_2D);
vramSetBankA(VRAM_A_MAIN_BG);
vramSetBankB(VRAM_B_MAIN_SPRITE);
vramSetBankD(VRAM_D_SUB_SPRITE);
oamInit(&oamMain, SpriteMapping_1D_128, false);
oamInit(&oamSub, SpriteMapping_1D_128, false);
#ifdef NDSA_AUDIO
mmInitDefaultMem( (mm_addr)mmsolution_bin );
#endif
#endif
#ifdef GBA
irqInit();
irqEnable(IRQ_VBLANK);
REG_IME = 1; // interrupts on
SetMode( OBJ_ON | BG0_ON );
#endif
Random.Seed();
}
bool Frame() {
#ifdef DS
swiWaitForVBlank();
TouchScreen.Update();
oamUpdate(&oamMain);
oamUpdate(&oamSub);
#endif
#ifdef GBA
setRepeat(1, 1);
VBlankIntrWait();
#endif
scanKeys();
Keys.keysDown = keysDown();
Keys.keysHeld = keysHeld();
// run through object code. the list can change as we go along.
// so we only run the current set of objects from the start of frame.
PointerList<Object> currentObjects = Objects.getCopy();
for(int c = 0; c < currentObjects.count; c++) {
Object *o = currentObjects.getByIndex(c);
if(Objects.found(o)) { // if it's still on the list
o->Step();
}
}
for(int c = 0; c < Colliders.count; c++) {
for(int d = c + 1; d < Colliders.count; d++) {
Object *o = Colliders.getByIndex(c);
Object *p = Colliders.getByIndex(d);
float ox1 = o->X + ((o->xSize - o->width) / 2), ox2 = ox1 + o->width;
float px1 = p->X + ((p->xSize - p->width) / 2), px2 = px1 + p->width;
float oy1 = o->Y + ((o->ySize - o->height) / 2), oy2 = oy1 + o->height;
float py1 = p->Y + ((p->ySize - p->height) / 2), py2 = py1 + p->height;
if (ox1 <= px2 && ox2 >= px1 &&
oy1 <= py2 && oy2 >= py1)
{
void (*function)(void*, void*) = (void(*)(void*,void*))CollisionEvents.getFunction(typeid(*o).name(), typeid(*p).name());
if(function) {
function(o, p);
} else {
function = (void(*)(void*,void*))CollisionEvents.getFunction(typeid(*p).name(), typeid(*o).name());
if(function) {
function(p, o);
}
}
}
}
}
return true;
}
} System;
}