summaryrefslogtreecommitdiff
path: root/lambda/lambda.c
blob: 380200018b96a324cd1f7dd34b6cac7897d20e92 (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
#include <HsFFI.h>
#ifdef __GLASGOW_HASKELL__
#include "Crackme_stub.h"
#endif

#include <ctype.h>
#include <math.h>

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>

#include "background.h"
#include "font.h"
#include "her3.h"

#define TITLE "Lambda"
#define WIDTH 640
#define HEIGHT 480

#define CHAR_WIDTH 32
#define CHAR_HEIGHT 32

SDL_Window *win;
SDL_Renderer *renderer;
SDL_Texture *font;
SDL_Texture *back;

char input[22];
char msg[] = "KEY ACCEPTED!";
int ptr = 0;
int should_quit = 0;
int key_accepted = 0;

char to_upper(char c)
{
		switch (c)
		{
		case '[':
				return '{';
		case ']':
				return '}';
		default:
				return toupper(c);
		}
}

void update_input(void)
{
		static SDL_Keymod mod;
		SDL_Event evt;
		SDL_PumpEvents();
		mod = SDL_GetModState();
		while (SDL_PollEvent(&evt)) {
				if (evt.type == SDL_KEYDOWN) {
						if (0x20 <= evt.key.keysym.sym && evt.key.keysym.sym <= 0x7f) {
								if (evt.key.repeat) {
										continue;
								}
								if (ptr < (int) sizeof(input) - 1){
										input[ptr++] = (mod & (KMOD_RSHIFT | KMOD_LSHIFT)) ? to_upper(evt.key.keysym.sym) : evt.key.keysym.sym;
								}
								if (keycheck_hs(input)) {
										key_accepted = 1;
								}
						} else if (evt.key.keysym.sym == SDLK_BACKSPACE) {
								if (evt.key.repeat) {
										continue;
								}
								if (ptr > 0) {
										input[--ptr] = ' ';
								}
						} else if (evt.key.keysym.sym == SDLK_ESCAPE) {
								should_quit = 1;
						}
				}
		}
}

void draw_char(int x, int y, char c)
{
		if (c < 0x20 || c > 0x7e) {
				return;
		}

		SDL_Rect src, dst;

		src.x = ((c - 0x20) % 16) * 8;
		src.y = ((c - 0x20) / 16) * 8;
		src.w = src.h = 8;

		dst.x = x;
		dst.y = y;
		dst.w = CHAR_WIDTH;
		dst.h = CHAR_HEIGHT;
		
		SDL_RenderCopy(renderer, font, &src, &dst);
}

int main(int argc, char **argv)
{
		int i;
		SDL_RWops *music_mem;
		SDL_RWops *font_mem;
		SDL_RWops *back_mem;
		Mix_Music *music;
		SDL_Surface *font_surf;
		SDL_Surface *back_surf;

		hs_init(&argc, &argv);

		for (i = 0; i < (int) sizeof(input); i++)
				input[i] = ' ';
		input[21] = '\0';

		if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
				fprintf(stderr, "Could not initialize SDL.\n");
				return 1;
		}
		if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) {
				fprintf(stderr, "Could not initialize SDL_image.\n");
				return 2;
		}
		if (Mix_Init(MIX_INIT_MOD) != MIX_INIT_MOD ||
			Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_CHANNELS, 4096) == -1) {
				fprintf(stderr, "Running without cool tunes :(\n");
		}

		win = SDL_CreateWindow(
		    TITLE,
			SDL_WINDOWPOS_UNDEFINED,
			SDL_WINDOWPOS_UNDEFINED,
			WIDTH,
			HEIGHT,
			0
		);
		if (win == NULL) {
				fprintf(stderr, "Could not initialize window.\n");
				return 3;
		}

		renderer = SDL_CreateRenderer(
		    win,
			-1,
			SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
		);
		if (renderer == NULL) {
				fprintf(stderr, "Could not initialize renderer.\n");
				return 4;
		}

		font_mem = SDL_RWFromMem(font_data, sizeof(font_data));
		back_mem = SDL_RWFromMem(background_data, sizeof(background_data));
		font_surf = IMG_LoadPNG_RW(font_mem);
		back_surf = IMG_LoadPNG_RW(back_mem);
		if (font_surf == NULL || back_surf == NULL) {
				fprintf(stderr, "Could not load images\n");
				return 5;
		}

		font = SDL_CreateTextureFromSurface(renderer, font_surf);
		back = SDL_CreateTextureFromSurface(renderer, back_surf);
		if (font == NULL || back == NULL) {
				fprintf(stderr, "Could not convert images to textures\n");
				return 6;
		}

		music_mem = SDL_RWFromMem(her3_data, sizeof(her3_data));
		music = Mix_LoadMUSType_RW(music_mem, MUS_MOD, sizeof(music_mem));
		if (music != NULL) {
				Mix_PlayMusic(music, -1);
		}

		double t = 0;

		while (!should_quit) {
				t += 1.5;
				update_input();
				SDL_RenderCopy(renderer, back, NULL, NULL);

				if (key_accepted)
						for (i = 0; i < (int) sizeof(msg); i++) {
								draw_char((20 - 13) / 2 * CHAR_WIDTH + CHAR_WIDTH + i * CHAR_WIDTH, 8 * cos(t / 256 + i) + HEIGHT / 4, msg[i]);
						}

				for (i = 0; i < (int) sizeof(input); i++) {
						draw_char((32 * i + (int) t) % (WIDTH + 4 * CHAR_WIDTH) - CHAR_WIDTH, 16 * sin(t / 128 + i) + HEIGHT / 2, input[i]);
				}
				
				SDL_RenderPresent(renderer);
				SDL_Delay(1000 / 60);
		}

		SDL_DestroyRenderer(renderer);
		SDL_DestroyWindow(win);
		IMG_Quit();
		Mix_Quit();
		SDL_Quit();
		hs_exit();
		return 0;
}