-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain_ncurses.c
164 lines (129 loc) · 3.43 KB
/
main_ncurses.c
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
/**
@file main_ncurses.c
This is ncurses (terminal) implementation of the game front end. This isn't
a full fledged version but rather a "show off" of what can be done with the
game; especially the limited ability of ncurses to handle input makes this
version very hard to play.
by Miloslav Ciz (drummyfish), 2024
Released under CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/)
plus a waiver of all other intellectual property. The goal of this work is to
be and remain completely in the public domain forever, available for any use
whatsoever.
*/
#include <ncurses.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#define SFG_SCREEN_RESOLUTION_X 120
#define SFG_SCREEN_RESOLUTION_Y 40
#define SFG_FPS 30
#include "game.h"
uint8_t ncScreen[SFG_SCREEN_RESOLUTION_X * SFG_SCREEN_RESOLUTION_Y];
uint8_t ncButtonStates[SFG_KEY_COUNT];
uint32_t timeStart;
const char asciiPalette[] =
#if 1
" .',:;lcoxkXK0MW";
#else
" -.,;!/clfsxaVO#";
#endif
uint32_t currentTime()
{
struct timespec t;
timespec_get(&t,TIME_UTC);
return 1000 * ((int64_t) t.tv_sec) + ((int64_t) t.tv_nsec) / 1000000;
}
int8_t SFG_keyPressed(uint8_t key)
{
return ncButtonStates[key];
}
void SFG_getMouseOffset(int16_t *x, int16_t *y)
{
return;
}
uint32_t SFG_getTimeMs()
{
return currentTime() - timeStart;
}
void SFG_sleepMs(uint16_t timeMs)
{
usleep(1000 * timeMs);
}
void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex)
{
ncScreen[y * SFG_SCREEN_RESOLUTION_X + x] = asciiPalette[colorIndex % 16];
}
void SFG_playSound(uint8_t soundIndex, uint8_t volume)
{
}
void SFG_setMusic(uint8_t value)
{
}
void SFG_processEvent(uint8_t event, uint8_t data)
{
}
void SFG_save(uint8_t data[SFG_SAVE_SIZE])
{
}
uint8_t SFG_load(uint8_t data[SFG_SAVE_SIZE])
{
return 0;
}
int main(void)
{
timeStart = currentTime();
initscr();
halfdelay(1);
keypad(stdscr,TRUE);
noecho();
curs_set(0);
SFG_init();
int goOn = 1;
while (goOn)
{
erase();
move(0,0);
const uint8_t *scr = ncScreen;
for (int y = 0; y < SFG_SCREEN_RESOLUTION_Y; ++y)
{
move(y,0);
for (int x = 0; x < SFG_SCREEN_RESOLUTION_X; ++x)
{
addch(*scr);
scr++;
}
}
refresh();
goOn = SFG_mainLoopBody();
for (int i = 0; i < SFG_KEY_COUNT; ++i)
ncButtonStates[i] = 0;
while (1)
{
int c = getch();
if (c == ERR)
break;
switch (c)
{
case KEY_UP: ncButtonStates[SFG_KEY_UP] = 1; break;
case KEY_LEFT: ncButtonStates[SFG_KEY_LEFT] = 1; break;
case KEY_RIGHT: ncButtonStates[SFG_KEY_RIGHT] = 1; break;
case KEY_DOWN: ncButtonStates[SFG_KEY_DOWN] = 1; break;
case 'a':
case KEY_ENTER: ncButtonStates[SFG_KEY_A] = 1; break;
case KEY_CANCEL:
case KEY_CLOSE:
case 's': ncButtonStates[SFG_KEY_B] = 1; break;
case 'd': ncButtonStates[SFG_KEY_C] = 1; break;
case ' ': ncButtonStates[SFG_KEY_JUMP] = 1; break;
case 'q': ncButtonStates[SFG_KEY_MENU] = 1; break;
case 'f': ncButtonStates[SFG_KEY_NEXT_WEAPON] = 1; break;
case 'g': ncButtonStates[SFG_KEY_PREVIOUS_WEAPON] = 1; break;
case 'n': ncButtonStates[SFG_KEY_STRAFE_LEFT] = 1; break;
case 'm': ncButtonStates[SFG_KEY_STRAFE_RIGHT] = 1; break;
default: break;
}
}
}
endwin();
return 0;
}