Index: proj/sprite.c
===================================================================
--- proj/sprite.c	(revision 162)
+++ proj/sprite.c	(revision 163)
@@ -3,17 +3,18 @@
 #include "sprite.h"
 
 #include "graphics.h"
+#include "utils.h"
 #include <math.h>
 
 struct sprite{
-    int x, y;
-    int w, h;
-    int u0, v0;
+    int16_t x, y;
+    uint16_t w, h;
+    int16_t u0, v0;
     double theta;
     uint8_t *map;
 };
 
-sprite_t* sprite_ctor(const char **xpm, int u0, int v0){
+sprite_t* (sprite_ctor)(const char **xpm, int u0, int v0){
     sprite_t *ret = (sprite_t*)malloc(sizeof(sprite_t));
     if(ret == NULL) return NULL;
     enum xpm_image_type type = XPM_8_8_8_8;
@@ -32,30 +33,24 @@
     ret->theta = 0;
     return ret;
 }
-void sprite_dtor(sprite_t *p){
+void (sprite_dtor)(sprite_t *p){
     if(p == NULL) return;
     if(p->map) free(p->map);
     free(p);
 }
 
-void sprite_set_x(sprite_t *p, int x){ p->x = x; }
-void sprite_set_y(sprite_t *p, int y){ p->y = y; }
-void sprite_set_pos(sprite_t *p, int x, int y){ sprite_set_x(p, x); sprite_set_y(p, y); }
-void sprite_set_angle(sprite_t *p, double angle){ p->theta = angle; }
-void sprite_set_center(sprite_t *p, int u0, int v0){ p->u0 = u0; p->v0 = v0; }
+void (sprite_set_x)     (sprite_t *p, int16_t x){ p->x = x; }
+void (sprite_set_y)     (sprite_t *p, int16_t y){ p->y = y; }
+void (sprite_set_pos)   (sprite_t *p, int16_t x, int16_t y){ sprite_set_x(p, x); sprite_set_y(p, y); }
+void (sprite_set_angle) (sprite_t *p, double angle){ p->theta = angle; }
+void (sprite_set_center)(sprite_t *p, int16_t u0, int16_t v0){ p->u0 = u0; p->v0 = v0; }
 
-int sprite_get_w(const sprite_t *p){ return p->w; }
-int sprite_get_h(const sprite_t *p){ return p->h; }
+uint16_t (sprite_get_w)(const sprite_t *p){ return p->w; }
+uint16_t (sprite_get_h)(const sprite_t *p){ return p->h; }
 
-void sprite_src2pic(const sprite_t *p, int x, int y, int *u, int *v){
-    int dx = x - p->x;
-    int dy = y - p->y;
-    //int du = dx*cos(p->theta) - dy*sin(p->theta);
-    //int dv = dx*sin(p->theta) + dy*cos(p->theta);
-    //*u = du + p->u0;
-    //*v = dv + p->v0;
-    //*u = dx*cos(p->theta) - dy*sin(p->theta) + p->u0;
-    //*v = dx*sin(p->theta) + dy*cos(p->theta) + p->v0;
+void (sprite_src2pic)(const sprite_t *p, int16_t x, int16_t y, int16_t *u, int16_t *v){
+    int16_t dx = x - p->x;
+    int16_t dy = y - p->y;
     double s = sin(p->theta);
     double c = cos(p->theta);
     *u = dx*c - dy*s + p->u0;
@@ -62,18 +57,16 @@
     *v = dx*s + dy*c + p->v0;
 }
 
-void sprite_draw(const sprite_t *p){
-    const int diag = sqrt(p->w*p->w + p->h*p->h)+2;
-    int u, v;
-    for(int y = p->y - diag; y < p->y + diag; ++y){
-        for(int x = p->x - diag; x < p->x + diag; ++x){
-            if (p->x + x < get_XRes() && p->y + y < get_YRes()){
-                sprite_src2pic(p, x, y, &u, &v);
-                if(0 <= u && u < p->w && 0 <= v && v < p->h){
-                    uint8_t *m = p->map + (v*p->w + u)*4;
-                    uint32_t color = SET_RGB(*(m+2), *(m+1), *(m));
-                    set_pixel_alpha(p->x + x, p->y + y, color, *(m+3));
-                }
+void (sprite_draw)(const sprite_t *p){
+    const int16_t diag = sqrt(p->w*p->w + p->h*p->h)+2;
+    int16_t u, v;
+    for(int16_t y = max(0,p->y-diag); y < min(p->y+diag,get_YRes()); ++y){
+        for(int16_t x = max(0,p->x-diag); x < min(p->x+diag,get_XRes()); ++x){
+            sprite_src2pic(p, x, y, &u, &v);
+            if(0 <= u && u < p->w && 0 <= v && v < p->h){
+                uint8_t *m = p->map + (v*p->w + u)*4;
+                uint32_t color = SET_RGB(*(m+2), *(m+1), *(m));
+                set_pixel_alpha(p->x + x, p->y + y, color, *(m+3));
             }
         }
     }
Index: proj/sprite.h
===================================================================
--- proj/sprite.h	(revision 162)
+++ proj/sprite.h	(revision 163)
@@ -4,18 +4,18 @@
 struct sprite;
 typedef struct sprite sprite_t;
 
-sprite_t* sprite_ctor(const char **xpm, int u0, int v0);
-void      sprite_dtor(sprite_t *p);
+sprite_t* (sprite_ctor)(const char **xpm, int u0, int v0);
+void      (sprite_dtor)(sprite_t *p);
 
-void sprite_set_x     (sprite_t *p, int x);
-void sprite_set_y     (sprite_t *p, int y);
-void sprite_set_pos   (sprite_t *p, int x, int y);
-void sprite_set_angle (sprite_t *p, double angle);
-void sprite_set_center(sprite_t *p, int u0, int v0);
+void (sprite_set_x)     (sprite_t *p, int16_t x);
+void (sprite_set_y)     (sprite_t *p, int16_t y);
+void (sprite_set_pos)   (sprite_t *p, int16_t x, int16_t y);
+void (sprite_set_angle) (sprite_t *p, double angle);
+void (sprite_set_center)(sprite_t *p, int16_t u0, int16_t v0);
 
-int sprite_get_w(const sprite_t *p);
-int sprite_get_h(const sprite_t *p);
+uint16_t (sprite_get_w)(const sprite_t *p);
+uint16_t (sprite_get_h)(const sprite_t *p);
 
-void sprite_draw(const sprite_t *p);
+void (sprite_draw)(const sprite_t *p);
 
 #endif //SPRITE_H_INCLUDED
