Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include "SDL.h"
  2.  
  3. /* utility functions */
  4. Uint32 pygame_list_to_color(TP,tp_obj clr,SDL_Surface *s) {
  5.     int r,g,b;
  6.     r = tp_get(tp,clr,tp_number(0)).number.val;
  7.     g = tp_get(tp,clr,tp_number(1)).number.val;
  8.     b = tp_get(tp,clr,tp_number(2)).number.val;
  9.     return SDL_MapRGB(s->format,r,g,b);
  10. }
  11.  
  12. /* surface */
  13.  
  14. #define PYGAME_TYPE_SURF 0x1001
  15.  
  16. void pygame_surf_free(TP,tp_obj d) {
  17.     if (d.data.magic != PYGAME_TYPE_SURF) { tp_raise(,"%s","not a surface"); }
  18.     SDL_FreeSurface((SDL_Surface*)d.data.val);
  19. }
  20.  
  21. SDL_Surface *pygame_obj_to_surf(TP,tp_obj self) {
  22.     tp_obj d = tp_get(tp,self,tp_string("__surf"));
  23.     if (d.data.magic != PYGAME_TYPE_SURF) { tp_raise(0,"%s","not a surface"); }
  24.     return (SDL_Surface*)d.data.val;
  25. }
  26.  
  27.  
  28. tp_obj pygame_surface_set_at(TP) {
  29.     tp_obj self = TP_OBJ();
  30.     tp_obj pos = TP_TYPE(TP_LIST);
  31.     tp_obj clr = TP_TYPE(TP_LIST);
  32.     SDL_Rect r;
  33.     r.x = tp_get(tp,pos,tp_number(0)).number.val;
  34.     r.y = tp_get(tp,pos,tp_number(1)).number.val;
  35.     r.w = 1; r.h = 1;
  36.     SDL_Surface *s =pygame_obj_to_surf(tp,self);
  37.     Uint32 c = pygame_list_to_color(tp,clr,s);
  38.     SDL_FillRect(s, &r, c);
  39.     return tp_None;
  40. }
  41.  
  42.  
  43. tp_obj pygame_surf_to_obj(TP,SDL_Surface *s) {
  44.     tp_obj self = tp_dict(tp);
  45.    
  46.     tp_obj d = tp_data(tp,PYGAME_TYPE_SURF,s);
  47.     d.data.info->free = pygame_surf_free;
  48.  
  49.     tp_set(tp,self,tp_string("__surf"),d);
  50.     tp_set(tp,self,tp_string("set_at"),tp_method(tp,self,pygame_surface_set_at));
  51.     return self;
  52. }
  53.  
  54.  
  55.  
  56.  
  57. /* display module */
  58.  
  59. tp_obj pygame_display_set_mode(TP) {
  60.     tp_obj sz = TP_TYPE(TP_LIST);
  61.     int w = tp_get(tp,sz,tp_number(0)).number.val;
  62.     int h = tp_get(tp,sz,tp_number(1)).number.val;
  63.     SDL_Surface *s = SDL_SetVideoMode(w, h, 0, 0);
  64.     return pygame_surf_to_obj(tp,s);
  65. }
  66.  
  67. tp_obj pygame_display_flip(TP) {
  68.     SDL_Flip(SDL_GetVideoSurface());
  69.     return tp_None;
  70. }
  71.  
  72. SDL_Rect pygame_list_to_rect(TP,tp_obj o) {
  73.     SDL_Rect r;
  74.     r.x = tp_get(tp,o,tp_number(0)).number.val;
  75.     r.y = tp_get(tp,o,tp_number(1)).number.val;
  76.     r.w = tp_get(tp,o,tp_number(2)).number.val;
  77.     r.h = tp_get(tp,o,tp_number(3)).number.val;
  78.     return r;
  79. }
  80.  
  81. tp_obj pygame_display_update(TP) {
  82.     SDL_Rect r = pygame_list_to_rect(tp,TP_TYPE(TP_LIST));
  83.     SDL_UpdateRects(SDL_GetVideoSurface(), 1, &r);
  84.     return tp_None;
  85. }
  86.  
  87. /* event module */
  88. tp_obj pygame_event_get(TP) {
  89.     SDL_Event e;
  90.     tp_obj r = tp_list(tp);
  91.     while (SDL_PollEvent(&e)) {
  92.         tp_obj d = tp_dict(tp);
  93.         tp_set(tp,d,tp_string("type"),tp_number(e.type));
  94.         switch (e.type) {
  95.             case SDL_KEYDOWN:
  96.             case SDL_KEYUP:
  97.                 tp_set(tp,d,tp_string("key"),tp_number(e.key.keysym.sym));
  98.                 tp_set(tp,d,tp_string("mod"),tp_number(e.key.keysym.mod));
  99.                 break;
  100.             case SDL_MOUSEMOTION:
  101.                 tp_set(tp,d,tp_string("pos"),tp_list_n(tp,2,(tp_obj[]){tp_number(e.motion.x),tp_number(e.motion.y)}));
  102.                 tp_set(tp,d,tp_string("rel"),tp_list_n(tp,2,(tp_obj[]){tp_number(e.motion.xrel),tp_number(e.motion.yrel)}));
  103.                 tp_set(tp,d,tp_string("state"),tp_number(e.motion.state));
  104.                 break;
  105.             case SDL_MOUSEBUTTONDOWN:
  106.             case SDL_MOUSEBUTTONUP:
  107.                 tp_set(tp,d,tp_string("pos"),tp_list_n(tp,2,(tp_obj[]){tp_number(e.button.x),tp_number(e.button.y)}));
  108.                 tp_set(tp,d,tp_string("button"),tp_number(e.button.button));
  109.                 break;
  110.         }
  111.         tp_set(tp,r,tp_None,d);
  112.     }
  113.     return r;
  114. }
  115.  
  116. /* mouse */
  117. tp_obj pygame_mouse_get_pos(TP) {
  118.     int x,y;
  119.     SDL_GetMouseState(&x,&y);
  120.     tp_obj r = tp_list_n(tp,2,(tp_obj[]){tp_number(x),tp_number(y)});
  121.     return r;
  122. }
  123.  
  124. /* time */
  125. tp_obj pygame_time_get_ticks(TP) {
  126.     return tp_number(SDL_GetTicks());
  127. }
  128.    
  129.  
  130. /* pygame */
  131. #define PYGAME_LOCALS(a,b) tp_set(tp,m,tp_string(a),tp_number(b));
  132.  
  133. tp_obj _pygame_init(TP) {
  134.     SDL_Init(SDL_INIT_VIDEO);
  135.     return tp_None;
  136. }
  137.  
  138.  
  139. void pygame_init(TP) {
  140.     tp_obj g,m;
  141.     g = tp_dict(tp);
  142.     tp_set(tp,tp->modules,tp_string("pygame"),g);
  143.     tp_set(tp,g,tp_string("init"),tp_fnc(tp,_pygame_init));
  144.    
  145.     /* display */
  146.     m = tp_dict(tp); tp_set(tp,g,tp_string("display"),m);
  147.     tp_set(tp,m,tp_string("set_mode"),tp_fnc(tp,pygame_display_set_mode));
  148.     tp_set(tp,m,tp_string("flip"),tp_fnc(tp,pygame_display_flip));
  149.     tp_set(tp,m,tp_string("update"),tp_fnc(tp,pygame_display_update));
  150.    
  151.     /* event */
  152.     m = tp_dict(tp); tp_set(tp,g,tp_string("event"),m);
  153.     tp_set(tp,m,tp_string("get"),tp_fnc(tp,pygame_event_get));
  154.    
  155.     /* locals */
  156.     m = tp_dict(tp); tp_set(tp,g,tp_string("locals"),m);
  157.     PYGAME_LOCALS("QUIT",SDL_QUIT);
  158.     PYGAME_LOCALS("KEYDOWN",SDL_KEYDOWN);
  159.     PYGAME_LOCALS("KEYUP",SDL_KEYUP);
  160.     PYGAME_LOCALS("MOUSEBUTTONDOWN",SDL_MOUSEBUTTONDOWN);
  161.     PYGAME_LOCALS("MOUSEBUTTONUP",SDL_MOUSEBUTTONUP);
  162.     PYGAME_LOCALS("MOUSEMOTION",SDL_MOUSEMOTION);
  163.     PYGAME_LOCALS("K_UP",SDLK_UP);
  164.     PYGAME_LOCALS("K_DOWN",SDLK_DOWN);
  165.     PYGAME_LOCALS("K_LEFT",SDLK_LEFT);
  166.     PYGAME_LOCALS("K_RIGHT",SDLK_RIGHT);
  167.     PYGAME_LOCALS("K_ESCAPE",SDLK_ESCAPE);
  168.     PYGAME_LOCALS("K_SPACE",SDLK_SPACE);
  169.     PYGAME_LOCALS("K_RETURN",SDLK_RETURN);
  170.    
  171.     /* mouse */
  172.     m = tp_dict(tp); tp_set(tp,g,tp_string("mouse"),m);
  173.     tp_set(tp,m,tp_string("get_pos"),tp_fnc(tp,pygame_mouse_get_pos));
  174.    
  175.     /* time */
  176.     m = tp_dict(tp); tp_set(tp,g,tp_string("time"),m);
  177.     tp_set(tp,m,tp_string("get_ticks"),tp_fnc(tp,pygame_time_get_ticks));
  178.    
  179.  
  180. }
  181.  
  182. /**/
  183.