-
Notifications
You must be signed in to change notification settings - Fork 1
/
simplenfl.h
198 lines (163 loc) · 5.8 KB
/
simplenfl.h
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
// SimpleNFL - Version 1.7
// By pipe
// With Help from KonPet, PolyMars and ngawung
// https://github.com/ppekko/SimpleNFL/
//
// Licensed under Creative Commons Attribution-NonCommercial 3.0
// Read it here: https://github.com/ppekko/SimpleNFL/blob/master/LICENSE
//
// The Docs/wiki is in the github repository
// Have fun!
#pragma once
#include <nds.h>
#include "nf_lib.h"
#ifdef __cplusplus
namespace SNF {
#endif
typedef enum { release, press, held } KeyPhase;
typedef enum { SwipeRight, SwipeLeft, SwipeUp, SwipeDown } Swipe;
static touchPosition Stylus;
static touchPosition PrevStylus; // previous frame stylus position
// store stylus position for swipe gesture
static int startX;
static int startY;
// quick load gfx and pallete to ram and vram
void loadsprite(int screen, int ramslot, int vramslot, int width, int height, const char *dir, const char *dir2, bool transflag) {
NF_LoadSpriteGfx(dir, ramslot, width, height);
NF_LoadSpritePal(dir2, ramslot);
NF_VramSpriteGfx(screen, ramslot, vramslot, transflag);
NF_VramSpritePal(screen, ramslot, vramslot);
}
// quick load gfx and pallete to ram and vram3D
void loadsprite3D(int ramslot, int vramslot, int width, int height, const char *dir, const char *dir2, bool transflag) {
NF_LoadSpriteGfx(dir, ramslot, width, height);
NF_LoadSpritePal(dir2, ramslot);
NF_Vram3dSpriteGfx(ramslot, vramslot, transflag);
NF_Vram3dSpritePal(ramslot, vramslot);
}
// unload gfx and pallete in ram and vram
void unloadsprite(int screen, int ramslot, int vramslot) {
// ram
NF_UnloadSpriteGfx(ramslot);
NF_UnloadSpritePal(ramslot);
// vram
NF_FreeSpriteGfx(screen, vramslot);
}
// unload gfx and pallete in ram and vram3D
void unloadsprite3D(int ramslot, int vramslot) {
// ram
NF_UnloadSpriteGfx(ramslot);
NF_UnloadSpritePal(ramslot);
// vram3D
NF_Free3dSpriteGfx(vramslot);
}
// quick load font and create font layer
void loadfont(const char *file, const char *fontname, int width, int height, int rot, int screen, int layer) {
NF_LoadTextFont(file, fontname, width, height, rot);
NF_CreateTextLayer(screen, layer, rot, fontname);
}
// quick load and create tiled background
void loadbg(const char *dir, const char *name, int width, int height, int screen, int layer) {
NF_LoadTiledBg(dir, name, width, height);
NF_CreateTiledBg(screen, layer, name);
}
// quick unload and remove tiled background
void unloadbg(int screen, int layer, const char *name) {
NF_DeleteTiledBg(screen, layer);
NF_UnloadTiledBg(name);
}
// quick load and play raw sound
void playandloadsound(const char *file, int channel, int freq, int sampleform, int vol, int pan, bool loop, int loopstart) {
NF_LoadRawSound(file, channel, freq, sampleform);
NF_PlayRawSound(channel, vol, pan, loop, loopstart);
}
// get touch input
bool getTouch(KeyPhase phase) {
touchRead(&Stylus);
switch (phase){
case release:
// store prev stylus because px,py return 0 when KeysUp()
if (keysHeld() & KEY_TOUCH) {
PrevStylus = Stylus;
}
if (keysUp() & KEY_TOUCH) {
Stylus = PrevStylus;
return true;
}
break;
case press:
return (keysDown() & KEY_TOUCH);
case held:
return (keysHeld() & KEY_TOUCH);
}
return false;
}
// get touch input inside rectangle
bool getTouchRect(int x, int y, int width, int height, KeyPhase phase) {
if (getTouch(phase)) {
int mathx = x + width - 1;
int mathy = y + height - 1;
return Stylus.px >= x && Stylus.px <= mathx && Stylus.py >= y && Stylus.py <= mathy;
}
return false;
}
// get touch input inside circle
bool getTouchCircle(int x, int y, int radius, KeyPhase phase) {
if (getTouch(phase)) {
return ((Stylus.px - x) * (Stylus.px - x)) + ((Stylus.py - y) * (Stylus.py - y)) < radius * radius;
}
return false;
}
// get simple swipe gesture
bool getSwipeGesture(Swipe gesture) {
if (getTouch(press)) {
startX = Stylus.px;
startY = Stylus.py;
}
if (getTouch(release)) {
int currentX = Stylus.px;
int currentY = Stylus.py;
int diffX = startX - currentX;
int diffY = startY - currentY;
// get absolute without math lib
int absX = diffX * ((diffX > 0) - (diffX < 0));
int absY = diffY * ((diffY > 0) - (diffY < 0));
bool result = absX > absY;
switch (gesture) {
case SwipeLeft:
return (result && diffX > 0);
case SwipeRight:
return (result && diffX < 0);
case SwipeUp:
return (!result && diffY > 0);
case SwipeDown:
return (!result && diffY < 0);
}
}
return false;
}
// check overlap of two rectangles
bool overlap(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
return ((x1 >= x2 && x1 <= x2 + w2 - 1 && y1 >= y2 && y1 <= y2 + h2 - 1) ||
(x1 + w1 - 1 >= x2 && x1 + w1 - 1 <= x2 + w2 - 1 && y1 >= y2 && y1 <= y2 + h2 - 1) ||
(x1 >= x2 && x1 <= x2 + w2 - 1 && y1 + h1 - 1 >= y2 && y1 + h1 - 1 <= y2 + h2 - 1) ||
(x1 + w1 - 1 >= x2 && x1 + w1 - 1 <= x2 + w2 - 1 && y1 + h1 - 1 >= y2 && y1 + h1 - 1 <= y2 + h2 - 1));
}
// get button input
bool getKeys(KEYPAD_BITS key, KeyPhase phase) {
switch (phase) {
case release:
return (keysUp() & key);
break;
case press:
return (keysDown() & key);
break;
case held:
return (keysHeld() & key);
break;
}
return false;
}
#ifdef __cplusplus
}
#endif