diff options
| author | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2021-04-06 12:23:00 -0400 |
|---|---|---|
| committer | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2021-04-06 12:25:17 -0400 |
| commit | 983cd29f5a2c3d9cd1d6a9c4f343adcffe54f88c (patch) | |
| tree | 395f4bf2dfe097d9d09413ad1b805765b2e7eae0 /lambda/lambda.c | |
Diffstat (limited to 'lambda/lambda.c')
| -rw-r--r-- | lambda/lambda.c | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/lambda/lambda.c b/lambda/lambda.c new file mode 100644 index 0000000..3802000 --- /dev/null +++ b/lambda/lambda.c @@ -0,0 +1,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; +} |