123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- #include <stdio.h>
- #include "sdl.h"
- #include "minilisp.h"
- #include "alloc.h"
- #include "stream.h"
- #include "compiler_new.h"
- #define WIDTH 800
- #define HEIGHT 600
- #define BPP 2
- #define DEPTH 8*BPP
- #define SCALE 1
- SDL_Surface* screen;
- uint8_t* pixels = NULL;
- void sdl_cleanup() {
- SDL_Quit();
- }
- static int sdl_initialized = 0;
- void* sdl_init(int fullscreen)
- {
- if (sdl_initialized) return screen->pixels;
- sdl_initialized = 1;
- SDL_Init(SDL_INIT_VIDEO);
- screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_SWSURFACE);
- //if (pixels) free(pixels);
- //pixels = malloc(WIDTH*HEIGHT*BPP);
-
- atexit(sdl_cleanup);
- return screen->pixels;
- return NULL;
- }
- static int sdl_key = 0;
- static int sdl_modifiers = 0;
- int sdl_get_key() {
- int k = sdl_key;
- sdl_key = 0;
- return k;
- }
- int sdl_get_modifiers() {
- int m = sdl_modifiers;
- return m;
- }
- void* sdl_get_fb() {
- if (screen) return screen->pixels;
- return NULL;
- }
- long sdl_get_fbsize() {
- return WIDTH*HEIGHT*BPP;
- }
- Cell* fbfs_open() {
- sdl_init(0);
- return alloc_int(1);
- }
- Cell* fbfs_read(Cell* stream) {
- Stream* s = (Stream*)stream->ar.addr;
- char* path = s->path->ar.addr;
- if (!strcmp(path+12,"/width")) {
- return alloc_int(WIDTH);
- }
- else if (!strcmp(path+12,"/height")) {
- return alloc_int(HEIGHT);
- }
- else if (!strcmp(path+12,"/depth")) {
- return alloc_int(BPP);
- }
- else if (!strcmp(path+12,"/")) {
- return
- alloc_cons(alloc_string_copy("/width"),
- alloc_cons(alloc_string_copy("/height"),
- alloc_cons(alloc_string_copy("/depth"),alloc_nil())));
- }
- else {
- return alloc_int(0);
- }
- }
- static int fb_state = 0;
- static int cursor_x = 0;
- static int cursor_y = 0;
- static int fb_count = 0;
- Cell* fbfs_write(Cell* arg) {
- //printf("[fbfs_write] %lx\n",arg->value);
- SDL_Event event;
- SDL_PollEvent(&event);
- //SDL_BlitScaled(pixels_surf,{0,0,WIDTH/SCALE,HEIGHT/SCALE},screen,{0,0,WIDTH,HEIGHT});
- SDL_UpdateRect(screen, 0, 0, WIDTH, HEIGHT);
- fb_count=0;
- return arg;
- }
- Cell* fbfs_mmap(Cell* arg) {
- Cell* fbtest = alloc_num_bytes(0);
- fbtest->ar.addr = sdl_get_fb();
- fbtest->dr.size = sdl_get_fbsize();
- //printf("fbtest->addr: %p\n",fbtest->addr);
- //printf("fbtest->size: %lx\n",fbtest->size);
- memset(fbtest->ar.addr,0xff,WIDTH*HEIGHT*BPP);
- return fbtest;
- }
- void sdl_mount_fbfs() {
- fs_mount_builtin("/framebuffer", fbfs_open, fbfs_read, fbfs_write, 0, fbfs_mmap);
- }
- Cell* keyfs_open() {
- return alloc_int(1);
- }
- Cell* keyfs_read() {
- SDL_Event event;
- if (SDL_PollEvent(&event))
- {
- //printf("sdl event! %d\n",event.type);
-
- switch (event.type)
- {
- case SDL_QUIT:
- exit(0);
- break;
- case SDL_KEYDOWN:
- sdl_modifiers = event.key.keysym.mod;
- sdl_key = event.key.keysym.sym;
- break;
- }
- }
- switch (sdl_key) {
- case 13: sdl_key = 10; break;
- case 8: sdl_key = 127; break;
- }
-
- Cell* res = alloc_string_copy(" ");
- ((uint8_t*)res->ar.addr)[0] = sdl_key;
- sdl_key = 0;
- return res;
- }
- void sdl_mount_keyfs() {
- fs_mount_builtin("/keyboard", keyfs_open, keyfs_read, 0, 0, 0);
- }
- void dev_sdl_init() {
- sdl_mount_fbfs();
- sdl_init(0);
-
- /*Cell* fbtest = alloc_num_bytes(0);
- fbtest->addr = sdl_get_fb();
- fbtest->size = sdl_get_fbsize();
- printf("fbtest->addr: %p\n",fbtest->addr);
- printf("fbtest->size: %lx\n",fbtest->size);
-
- insert_global_symbol(alloc_sym("fb"), fbtest);*/
- sdl_mount_keyfs();
- }
|