sdl.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <stdio.h>
  2. #include "sdl.h"
  3. #include "minilisp.h"
  4. #include "alloc.h"
  5. #include "stream.h"
  6. #include "compiler_new.h"
  7. #define WIDTH 800
  8. #define HEIGHT 600
  9. #define BPP 2
  10. #define DEPTH 8*BPP
  11. #define SCALE 1
  12. SDL_Surface* screen;
  13. uint8_t* pixels = NULL;
  14. void sdl_cleanup() {
  15. SDL_Quit();
  16. }
  17. static int sdl_initialized = 0;
  18. void* sdl_init(int fullscreen)
  19. {
  20. if (sdl_initialized) return screen->pixels;
  21. sdl_initialized = 1;
  22. SDL_Init(SDL_INIT_VIDEO);
  23. screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_SWSURFACE);
  24. //if (pixels) free(pixels);
  25. //pixels = malloc(WIDTH*HEIGHT*BPP);
  26. atexit(sdl_cleanup);
  27. return screen->pixels;
  28. return NULL;
  29. }
  30. static int sdl_key = 0;
  31. static int sdl_modifiers = 0;
  32. int sdl_get_key() {
  33. int k = sdl_key;
  34. sdl_key = 0;
  35. return k;
  36. }
  37. int sdl_get_modifiers() {
  38. int m = sdl_modifiers;
  39. return m;
  40. }
  41. void* sdl_get_fb() {
  42. if (screen) return screen->pixels;
  43. return NULL;
  44. }
  45. long sdl_get_fbsize() {
  46. return WIDTH*HEIGHT*BPP;
  47. }
  48. Cell* fbfs_open() {
  49. sdl_init(0);
  50. return alloc_int(1);
  51. }
  52. Cell* fbfs_read(Cell* stream) {
  53. Stream* s = (Stream*)stream->ar.addr;
  54. char* path = s->path->ar.addr;
  55. if (!strcmp(path+12,"/width")) {
  56. return alloc_int(WIDTH);
  57. }
  58. else if (!strcmp(path+12,"/height")) {
  59. return alloc_int(HEIGHT);
  60. }
  61. else if (!strcmp(path+12,"/depth")) {
  62. return alloc_int(BPP);
  63. }
  64. else if (!strcmp(path+12,"/")) {
  65. return
  66. alloc_cons(alloc_string_copy("/width"),
  67. alloc_cons(alloc_string_copy("/height"),
  68. alloc_cons(alloc_string_copy("/depth"),alloc_nil())));
  69. }
  70. else {
  71. return alloc_int(0);
  72. }
  73. }
  74. static int fb_state = 0;
  75. static int cursor_x = 0;
  76. static int cursor_y = 0;
  77. static int fb_count = 0;
  78. Cell* fbfs_write(Cell* arg) {
  79. //printf("[fbfs_write] %lx\n",arg->value);
  80. SDL_Event event;
  81. SDL_PollEvent(&event);
  82. //SDL_BlitScaled(pixels_surf,{0,0,WIDTH/SCALE,HEIGHT/SCALE},screen,{0,0,WIDTH,HEIGHT});
  83. SDL_UpdateRect(screen, 0, 0, WIDTH, HEIGHT);
  84. fb_count=0;
  85. return arg;
  86. }
  87. Cell* fbfs_mmap(Cell* arg) {
  88. Cell* fbtest = alloc_num_bytes(0);
  89. fbtest->ar.addr = sdl_get_fb();
  90. fbtest->dr.size = sdl_get_fbsize();
  91. //printf("fbtest->addr: %p\n",fbtest->addr);
  92. //printf("fbtest->size: %lx\n",fbtest->size);
  93. memset(fbtest->ar.addr,0xff,WIDTH*HEIGHT*BPP);
  94. return fbtest;
  95. }
  96. void sdl_mount_fbfs() {
  97. fs_mount_builtin("/framebuffer", fbfs_open, fbfs_read, fbfs_write, 0, fbfs_mmap);
  98. }
  99. Cell* keyfs_open() {
  100. return alloc_int(1);
  101. }
  102. Cell* keyfs_read() {
  103. SDL_Event event;
  104. if (SDL_PollEvent(&event))
  105. {
  106. //printf("sdl event! %d\n",event.type);
  107. switch (event.type)
  108. {
  109. case SDL_QUIT:
  110. exit(0);
  111. break;
  112. case SDL_KEYDOWN:
  113. sdl_modifiers = event.key.keysym.mod;
  114. sdl_key = event.key.keysym.sym;
  115. break;
  116. }
  117. }
  118. switch (sdl_key) {
  119. case 13: sdl_key = 10; break;
  120. case 8: sdl_key = 127; break;
  121. }
  122. Cell* res = alloc_string_copy(" ");
  123. ((uint8_t*)res->ar.addr)[0] = sdl_key;
  124. sdl_key = 0;
  125. return res;
  126. }
  127. void sdl_mount_keyfs() {
  128. fs_mount_builtin("/keyboard", keyfs_open, keyfs_read, 0, 0, 0);
  129. }
  130. void dev_sdl_init() {
  131. sdl_mount_fbfs();
  132. sdl_init(0);
  133. /*Cell* fbtest = alloc_num_bytes(0);
  134. fbtest->addr = sdl_get_fb();
  135. fbtest->size = sdl_get_fbsize();
  136. printf("fbtest->addr: %p\n",fbtest->addr);
  137. printf("fbtest->size: %lx\n",fbtest->size);
  138. insert_global_symbol(alloc_sym("fb"), fbtest);*/
  139. sdl_mount_keyfs();
  140. }