Project

General

Profile

Revision 279

more changes of place

View differences:

proj/include/xpm_utils.h
1
#ifndef XMP_UTILS_H_INCLUDED
2
#define XMP_UTILS_H_INCLUDED
3

  
4
void xpm_save_as_xpm2(const char **p, const char *s);
5

  
6
char** xpm_load_xpm2(const char *s);
7

  
8
#endif //XMP_UTILS_H_INCLUDED
9 0

  
proj/include/fast_math.h
1
#ifndef FAST_MATH_H_INCLUDED
2
#define FAST_MATH_H_INCLUDED
3

  
4
double fm_sin(double x);
5
double fm_cos(double x);
6

  
7
#endif //FAST_MATH_H_INCLUDED
8 0

  
proj/include/utils.h
1
#ifndef UTILS_H_INCLUDED
2
#define UTILS_H_INCLUDED
3

  
4
#define BCD_FIRST(n)     (n >> 4)       /** @brief Get first digit (leftmost digit) of 8-bit BCD */
5
#define BCD_SECOND(n)    (n & 0x0F)     /** @brief Get second digit (rightmost digit) of 8-bit BCD */
6

  
7
/**
8
 * @brief Gets the least significant byte of a 16-bit variable
9
 * @param val 16-bit variable
10
 * @param lsb Pointer to a 8-bit variable to store the value of the LSB
11
 * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
12
 */
13
int(util_get_LSB)(uint16_t val, uint8_t *lsb);
14

  
15
/**
16
 * @brief Gets the most significant byte of a 16-bit variable
17
 * @param val 16-bit variable
18
 * @param lsb Pointer to a 8-bit variable to store the value of the MSB
19
 * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
20
 */
21
int(util_get_MSB)(uint16_t val, uint8_t *msb);
22

  
23
/**
24
 * @brief sys_inb wrapper
25
 * @param port Port to read from
26
 * @param value Pointer to byte to store value read
27
 * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
28
 */
29
int (util_sys_inb)(int port, uint8_t *value);
30

  
31
/**
32
 * @brief Unsubcribes Interrupts
33
 * @param interrupt_id Interrupt ID, value via arguments on subscription of the interrupt_id
34
 * @see subscribe_kbc_interrupt, subscribe_timer_interrupt
35
 * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
36
 */
37
int (unsubscribe_interrupt)(int *interrupt_id);
38

  
39
/**
40
 * @brief Gets the minimum value out of two values.
41
 * @param a     First value
42
 * @param b     Second value
43
 * @return  The minimum of the two values
44
 */
45
int32_t min(int32_t a, int32_t b);
46

  
47
/**
48
 * @brief Gets the maximum value out of two values.
49
 * @param a     First value
50
 * @param b     Second value
51
 * @return  The maximum of the two values
52
 */
53
int32_t max(int32_t a, int32_t b);
54

  
55
/**
56
 * @brief Gets the minimum value out of two doubles.
57
 * @param a     First value
58
 * @param b     Second value
59
 * @return  The minimum of the two values
60
 */
61
double min_d(double a, double b);
62

  
63
/**
64
 * @brief Gets the maximum value out of two doubles.
65
 * @param a     First value
66
 * @param b     Second value
67
 * @return  The maximum of the two values
68
 */
69
double max_d(double a, double b);
70

  
71

  
72
double abs_d(double a);
73

  
74

  
75
#endif //UTILS_H_INCLUDED
76 0

  
proj/include/graph.h
1
#ifndef GRAPH_H_INCLUDED
2
#define GRAPH_H_INCLUDED
3

  
4
// Graphics modes
5
#define INDEXED_1024_768        0x105
6
#define DIRECT_640_480_888      0x110
7
#define DIRECT_800_600_888      0x115
8
#define DIRECT_1024_768_888     0x118
9
#define DIRECT_1280_1024_565    0x11A
10
#define DIRECT_1280_1024_888    0x11B
11
#define LINEAR_FRAME_BUFFER_MD  BIT(14)
12

  
13
// Colors in RBG (8 bit)
14
#define GRAPH_BLACK               0x000000
15
#define GRAPH_WHITE               0xFFFFFF
16

  
17
// Alpha
18
#define ALPHA_THRESHOLD     0x7F
19

  
20
/// MACROS
21
#define GET_ALP(n)          (0xFF & ((n) >> 24))
22
#define GET_RED(n)          (0xFF & ((n) >> 16))
23
#define GET_GRE(n)          (0xFF & ((n) >>  8))
24
#define GET_BLU(n)          (0xFF & (n      ))
25
#define GET_COLOR(n)        (0xFFFFFF & (n))
26
#define SET_ALP(n)          (((n)&0xFF) << 24)
27
#define SET_RED(n)          (((n)&0xFF) << 16)
28
#define SET_GRE(n)          (((n)&0xFF) <<  8)
29
#define SET_BLU(n)          (((n)&0xFF)      )
30
#define SET_RGB(r,g,b)      (SET_RED(r) | SET_GRE(g) | SET_BLU(b))
31

  
32
/// PUBLIC GET
33
uint16_t   (graph_get_XRes)         (void);
34
uint16_t   (graph_get_YRes)         (void);
35
uint16_t   (graph_get_bytes_pixel)  (void);
36

  
37
/// INIT
38
int (graph_init)(uint16_t mode);
39

  
40
/// CLEANUP
41
int (graph_cleanup)(void);
42

  
43
/// PIXEL DRAWING
44
int (graph_set_pixel)             (uint16_t x, uint16_t y, uint32_t color);
45
void (graph_set_pixel_pos)         (unsigned pos          , uint32_t color);
46

  
47
/// SCREEN
48
int (graph_clear_screen)(void);
49

  
50
/// DRAW
51
int (graph_draw)(void);
52

  
53
#endif /* end of include guard: GRAPH_H_INCLUDED */
54 0

  
proj/src/graph.c
1
#include <lcom/lcf.h>
2

  
3
#include "graph.h"
4

  
5
#include "errors.h"
6
#include <stdio.h>
7

  
8
#define VC_BIOS_SERV  0x10 /** @brief Interrupt service of video card */
9
#define VBE_CALL      0x4F /** @brief VBE call function specifier */
10

  
11
#define MBYTE_BASE  0x0         /** @brief Base address (zero address) */
12
#define MBYTE_SIZE  0xFFFFF     /** @brief Size of a mebibyte */
13

  
14
// Graphics Functions
15
#define VBE_CTRL_INFO       0x00    /** @brief Get VBE Controller Information */
16
#define VBE_MD_INFO         0x01    /** @brief Get VBE Mode Information */
17
#define SET_VBE_MD          0x02    /** @brief Set VBE Mode */
18

  
19
// Error codes (AH)
20
#define AH_SUCCESS          0x00    /** @brief Success code on BIOS call */
21
#define AH_FUNC_CALL_FAIL   0x01    /** @brief Function call failed */
22
#define AH_FUNC_NOT_SUPP    0x02    /** @brief Function call is not supported in current HW configuration */
23
#define AH_FUNC_INVALID     0x03    /** @brief Invalid function in current video mode */
24

  
25
/// MACROS
26
#define FAR2PHYS(n)         ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF))
27

  
28
/// STRUCT
29
typedef struct __attribute__((packed)) {
30

  
31
    char        VbeSignature[4]     ;
32
    uint16_t    VbeVersion          ;
33
    uint32_t    OemStringPtr        ;
34
    uint8_t     Capabilities[4]     ;
35
    uint32_t    VideoModePtr        ;
36
    uint16_t    TotalMemory         ;
37

  
38
    uint16_t    OemSoftwareRev      ;
39
    uint32_t    OemVendorNamePtr    ;
40
    uint32_t    OemProductNamePtr   ;
41
    uint32_t    OemProductRevPtr    ;
42
    char        Reserved[222]       ;
43

  
44
    char        OemData[256]        ;
45
} VbeInfoBlock;
46

  
47
static vbe_mode_info_t vbe_mem_info;
48

  
49
/// PRIVATE GET
50
static uint16_t   (graph_get_bits_pixel)   (void){ return vbe_mem_info.BitsPerPixel; }
51
static phys_bytes (graph_get_phys_addr)    (void){ return vbe_mem_info.PhysBasePtr; }
52
static unsigned   (graph_get_vram_size)    (void){ return vbe_mem_info.XResolution * vbe_mem_info.YResolution * graph_get_bytes_pixel(); }
53
//static uint16_t   (graph_get_RedMaskSize)  (void){ return vbe_mem_info.RedMaskSize  ; }
54
//static uint16_t   (graph_get_GreenMaskSize)(void){ return vbe_mem_info.GreenMaskSize; }
55
//static uint16_t   (graph_get_BlueMaskSize) (void){ return vbe_mem_info.BlueMaskSize ; }
56

  
57
/// PUBLIC GET
58
uint16_t   (graph_get_XRes)         (void){ return vbe_mem_info.XResolution; }
59
uint16_t   (graph_get_YRes)         (void){ return vbe_mem_info.YResolution; }
60
uint16_t   (graph_get_bytes_pixel)  (void){ return (graph_get_bits_pixel() + 7) >> 3; }
61

  
62
///
63
static int (get_permission)(unsigned int base_addr, unsigned int size) {
64
    struct minix_mem_range mmr;
65
    mmr.mr_base = base_addr;
66
    mmr.mr_limit = base_addr + size;
67
    return sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr);
68
}
69

  
70
//static int (get_permissions_first_mbyte)(void) {
71
//    return get_permission(MBYTE_BASE, MBYTE_SIZE);
72
//}
73

  
74
/// MEMORY
75
static uint8_t *video_mem = NULL; /** @brief Frame-buffer VM address. */
76
static uint8_t *video_buf = NULL; /** @brief Primary buffer for drawing before copying to video_mem. */
77
static mmap_t mem_map;
78
static int (graph_free_memory)(void) {
79
    int r = SUCCESS;
80
    free(video_buf); video_buf = NULL;
81
    r = !lm_free(&mem_map);
82
    return r;
83
}
84
static int (graph_map_vram)(void) {
85
    int r;
86
    const unsigned vram_base = graph_get_phys_addr();
87
    const unsigned vram_size = graph_get_vram_size();
88
    if ((r = get_permission(vram_base, vram_size))) {
89
        if (graph_free_memory()) {
90
            printf("%s: lm_free failed\n", __func__);
91
        }
92
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
93
    }
94

  
95
    video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
96

  
97
    if (video_mem == MAP_FAILED) {
98
        if (graph_free_memory()) {
99
            printf("%s: lm_free failed\n", __func__);
100
        }
101
        panic("%s: couldn't map video memory.", __func__);
102
    }
103

  
104
    video_buf = malloc(vram_size);
105

  
106
    return SUCCESS;
107
}
108

  
109
/// INFO GET
110
static int (vbe_get_mode_information)(uint16_t mode) {
111
    memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // reset values
112

  
113
    struct reg86 reg_86;
114
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
115

  
116
    vbe_mode_info_t *virtual_addr = lm_alloc(sizeof(vbe_mode_info_t), &mem_map);
117

  
118
    reg_86.intno = VC_BIOS_SERV;
119
    reg_86.ah = VBE_CALL;
120
    reg_86.al = VBE_MD_INFO;
121
    reg_86.cx = mode;
122
    reg_86.es = PB2BASE(mem_map.phys);
123
    reg_86.di = PB2OFF(mem_map.phys);
124
    // BIOS CALL
125
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
126
        printf("%s: sys_int86 failed\n", __func__);
127
        if (graph_free_memory()) {
128
            printf("%s: lm_free failed\n", __func__);
129
        }
130
        return BIOS_CALL_ERROR;
131
    }
132

  
133
    memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size);
134
    return SUCCESS;
135
}
136
/*
137
static int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) {
138
    memset(info_p, 0, sizeof(vg_vbe_contr_info_t)); // reset values
139

  
140
    mmap_t controller_map;
141

  
142
    struct reg86 reg_86;
143
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
144

  
145
    VbeInfoBlock *virtual_addr = lm_alloc(sizeof(VbeInfoBlock), &controller_map);
146

  
147
    uint32_t virtual_base = (uint32_t)(virtual_addr) - controller_map.phys;
148

  
149
    virtual_addr->VbeSignature[0] = 'V';
150
    virtual_addr->VbeSignature[1] = 'B';
151
    virtual_addr->VbeSignature[2] = 'E';
152
    virtual_addr->VbeSignature[3] = '2';
153

  
154

  
155
    reg_86.intno = VC_BIOS_SERV;
156
    reg_86.ah = VBE_CALL;
157
    reg_86.al = VBE_CTRL_INFO;
158
    reg_86.es = PB2BASE(controller_map.phys);
159
    reg_86.di = PB2OFF(controller_map.phys);
160
    // BIOS CALL
161
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
162
        printf("%s: sys_int86 failed\n", __func__);
163
        if (!lm_free(&controller_map)) {
164
            printf("%s: lm_free failed\n", __func__);
165
        }
166
        return BIOS_CALL_ERROR;
167
    }
168

  
169
    info_p->VBESignature[0] = virtual_addr->VbeSignature[0];
170
    info_p->VBESignature[1] = virtual_addr->VbeSignature[1];
171
    info_p->VBESignature[2] = virtual_addr->VbeSignature[2];
172
    info_p->VBESignature[3] = virtual_addr->VbeSignature[3];
173

  
174
    uint8_t lsb, msb;
175
    util_get_LSB(virtual_addr->VbeVersion, &lsb);
176
    util_get_MSB(virtual_addr->VbeVersion, &msb);
177
    info_p->VBEVersion[0] = lsb;
178
    info_p->VBEVersion[1] = msb;
179

  
180
    info_p->TotalMemory = (virtual_addr->TotalMemory << 6);
181

  
182
    // Convert Far Far Pointer to Virtual Address
183

  
184
    uint32_t phys_ptr = FAR2PHYS(virtual_addr->OemStringPtr);
185
    uint32_t virtual_ptr = phys_ptr + virtual_base;
186
    info_p->OEMString = (char*)(virtual_ptr);
187

  
188
    phys_ptr = FAR2PHYS(virtual_addr->VideoModePtr);
189
    virtual_ptr = phys_ptr + virtual_base;
190
    info_p->VideoModeList = (uint16_t*)(virtual_ptr);
191

  
192
    phys_ptr = FAR2PHYS(virtual_addr->OemVendorNamePtr);
193
    virtual_ptr = phys_ptr + virtual_base;
194
    info_p->OEMVendorNamePtr = (char*)(virtual_ptr);
195

  
196
    phys_ptr = FAR2PHYS(virtual_addr->OemProductNamePtr);
197
    virtual_ptr = phys_ptr + virtual_base;
198
    info_p->OEMProductNamePtr = (char*)(virtual_ptr);
199

  
200
    phys_ptr = FAR2PHYS(virtual_addr->OemProductRevPtr);
201
    virtual_ptr = phys_ptr + virtual_base;
202
    info_p->OEMProductRevPtr = (char*)(virtual_ptr);
203

  
204
    if (!lm_free(&controller_map)) {
205
        printf("%s: lm_free failed\n", __func__);
206
        return LCF_ERROR;
207
    }
208

  
209
    return SUCCESS;
210
}
211
*/
212

  
213
/// INIT
214
/**
215
 * @brief
216
 * @param mode
217
 * @return
218
 */
219
static int (graph_set_mode)(uint16_t mode) {
220
    struct reg86 reg_86;
221

  
222
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
223

  
224
    // Set Reg86
225
    reg_86.intno = VC_BIOS_SERV;
226
    reg_86.ah = VBE_CALL;
227
    reg_86.al = SET_VBE_MD;
228
    reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD;
229

  
230
    // BIOS CALL
231
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
232
        printf("%s: sys_int86 failed\n", __func__);
233
        return BIOS_CALL_ERROR;
234
    }
235

  
236
    return SUCCESS;
237
}
238
int (graph_init)(uint16_t mode){
239
    if (vbe_get_mode_information(mode)) {
240
        printf("%s: failed to get information for mode %x.\n", __func__, mode);
241
        return 1;
242
    }
243

  
244
    graph_map_vram(); // if function fails it aborts program
245

  
246
    if (graph_set_mode(mode)) {
247
        printf("%s: failed to set graphic mode %x.\n", __func__, mode);
248
        return 1;
249
    };
250
    return SUCCESS;
251
}
252

  
253
/// CLEANUP
254
int (graph_cleanup)(void){
255
    int r = SUCCESS;
256
    if ((r = vg_exit()))
257
        printf("%s: vg_exit failed to exit to text mode.\n", __func__);
258
    if ((r = graph_free_memory()))
259
        printf("%s: lm_free failed\n", __func__);
260
    return r;
261
}
262

  
263
/// PIXEL DRAWING
264
int (graph_set_pixel)(uint16_t x, uint16_t y, uint32_t color) {
265
    //pixels are certain to be inside can reduce lag
266
    /*if (x < 0 || vbe_mem_info.XResolution <= x || y < 0 || vbe_mem_info.YResolution <= y) {
267
        //printf("%s: invalid pixel.\n", __func__);
268
        return OUT_OF_RANGE;
269
    }*/
270
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * 3/*graph_get_bytes_pixel()*/;
271
    memcpy(video_buf + pos, &color, 3/*graph_get_bytes_pixel()*/);
272
    return SUCCESS;
273
}
274
void (graph_set_pixel_pos)(unsigned pos, uint32_t color){
275
    memcpy(video_buf + pos, &color, graph_get_bytes_pixel());
276
}
277
int (graph_clear_screen)(void){ memset(video_buf, 0, graph_get_vram_size()); return SUCCESS; }
278
int (graph_draw)(void){ memcpy(video_mem, video_buf, graph_get_vram_size()); return SUCCESS; }
279

  
280
///SPRITE
281
#include "sprite.h"
282

  
283
#include "utils.h"
284
#include "fast_math.h"
285
#include <math.h>
286

  
287
struct basic_sprite{
288
    uint8_t *map;
289
    uint16_t w, h;
290
    int16_t u0, v0;
291
};
292
basic_sprite_t* (basic_sprite_ctor)(const char **xpm, int16_t u0, int16_t v0){
293
    basic_sprite_t *ret = malloc(sizeof(basic_sprite_t));
294
    if(ret == NULL) return NULL;
295
    enum xpm_image_type type = XPM_8_8_8_8;
296
    xpm_image_t img;
297
    ret->map = xpm_load((xpm_map_t)xpm, type, &img);
298
    if(ret->map == NULL){
299
        basic_sprite_dtor(ret);
300
        return NULL;
301
    }
302
    ret->w = img.width;
303
    ret->h = img.height;
304
    ret->u0 = u0;
305
    ret->v0 = v0;
306
    return ret;
307
}
308
void (basic_sprite_dtor)(basic_sprite_t *p){
309
    if(p == NULL) return;
310
    free(p->map);
311
    free(p);
312
}
313
const uint8_t* (basic_sprite_get_map)(const basic_sprite_t *p){ return p->map; }
314
uint16_t       (basic_sprite_get_w)  (const basic_sprite_t *p){ return p->w  ; }
315
uint16_t       (basic_sprite_get_h)  (const basic_sprite_t *p){ return p->h  ; }
316
int16_t        (basic_sprite_get_u0) (const basic_sprite_t *p){ return p->u0 ; }
317
int16_t        (basic_sprite_get_v0) (const basic_sprite_t *p){ return p->v0 ; }
318

  
319
/*
320
struct basic_sprite_alpha{
321
    uint8_t *map;
322
    uint16_t w, h;
323
    int16_t u0, v0;
324
};
325
basic_sprite_alpha_t* (basic_sprite_alpha_ctor)(const char **xpm, int16_t u0, int16_t v0){
326
    basic_sprite_alpha_t *ret = malloc(sizeof(basic_sprite_t));
327
    if(ret == NULL) return NULL;
328
    enum xpm_image_type type = XPM_8_8_8_8;
329
    xpm_image_t img;
330
    ret->map = NULL;
331
    uint8_t *m = xpm_load((xpm_map_t)xpm, type, &img);
332
    if(m == NULL){
333
        basic_sprite_alpha_dtor(ret);
334
        return NULL;
335
    }
336
    ret->map = m;
337
    if(ret->map == NULL){
338
        basic_sprite_alpha_dtor(ret);
339
        return NULL;
340
    }
341
    ret->w = img.width;
342
    ret->h = img.height;
343
    ret->u0 = u0;
344
    ret->v0 = v0;
345
    return ret;
346
}
347
void (basic_sprite_alpha_dtor)(basic_sprite_alpha_t *p){
348
    if(p == NULL) return;
349
    free(p->map);
350
    free(p);
351
}
352
const uint8_t* (basic_sprite_alpha_get_map)(const basic_sprite_alpha_t *p){ return p->map; }
353
uint16_t       (basic_sprite_alpha_get_w)  (const basic_sprite_alpha_t *p){ return p->w  ; }
354
uint16_t       (basic_sprite_alpha_get_h)  (const basic_sprite_alpha_t *p){ return p->h  ; }
355
int16_t        (basic_sprite_alpha_get_u0) (const basic_sprite_alpha_t *p){ return p->u0 ; }
356
int16_t        (basic_sprite_alpha_get_v0) (const basic_sprite_alpha_t *p){ return p->v0 ; }
357
*/
358

  
359
struct sprite{
360
    const basic_sprite_t *bsp;
361
    int16_t x, y; //position in screen
362
    double theta, s, c;
363
    double scale;
364
};
365
sprite_t* (sprite_ctor)(const basic_sprite_t *bsp){
366
    sprite_t *ret = malloc(sizeof(sprite_t));
367
    if(ret == NULL) return NULL;
368
    ret->bsp = bsp;
369
    ret->x = 0;
370
    ret->y = 0;
371
    sprite_set_angle(ret, 0.0);
372
    ret->scale = 1.0;
373
    return ret;
374
}
375
void (sprite_dtor)(sprite_t *p){
376
    if(p == NULL) return;
377
    free(p);
378
}
379
void (sprite_set_pos)   (sprite_t *p, int16_t x , int16_t y ){ p->x = x; p->y = y; }
380
void (sprite_set_angle) (sprite_t *p, double angle          ){ p->theta = angle; p->c = fm_cos(p->theta); p->s = fm_sin(p->theta); }
381
void (sprite_set_scale) (sprite_t *p, double scale          ){ p->scale = scale; }
382
int16_t  (sprite_get_x)(const sprite_t *p){ return p->x; }
383
int16_t  (sprite_get_y)(const sprite_t *p){ return p->y; }
384
double   (sprite_get_angle)(const sprite_t *p){ return p->theta; }
385
uint16_t (sprite_get_w)(const sprite_t *p){ return basic_sprite_get_w(p->bsp); }
386
uint16_t (sprite_get_h)(const sprite_t *p){ return basic_sprite_get_h(p->bsp); }
387
void (sprite_src2pic)(const sprite_t *p, int16_t x, int16_t y, int16_t *u, int16_t *v){
388
    double dx = (x - p->x)/p->scale;
389
    double dy = (y - p->y)/p->scale;
390
    int16_t du = dx*p->c - dy*p->s - 0.5;
391
    int16_t dv = dx*p->s + dy*p->c - 0.5;
392
    *u = du + basic_sprite_get_u0(p->bsp);
393
    *v = dv + basic_sprite_get_v0(p->bsp);
394
}
395
void (sprite_pic2src)(const sprite_t *p, int16_t u, int16_t v, int16_t *x, int16_t *y){
396
    int16_t du = u - basic_sprite_get_u0(p->bsp);
397
    int16_t dv = v - basic_sprite_get_v0(p->bsp);
398
    double dx =  du*p->c + dv*p->s;
399
    double dy = -du*p->s + dv*p->c;
400
    *x = dx*p->scale + 0.5 + p->x;
401
    *y = dy*p->scale + 0.5 + p->y;
402
}
403
void (sprite_draw)(const sprite_t *p){
404
    const uint16_t w = basic_sprite_get_w(p->bsp);
405
    const uint16_t h = basic_sprite_get_h(p->bsp);
406
    int16_t xmin, xmax, ymin, ymax; {
407
        int16_t x, y;
408
        sprite_pic2src(p, 0, 0, &x, &y);
409
        xmin = x; xmax = x; ymin = y; ymax = y;
410
        sprite_pic2src(p, w, 0, &x, &y);
411
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
412
        sprite_pic2src(p, 0, h, &x, &y);
413
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
414
        sprite_pic2src(p, w, h, &x, &y);
415
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
416
        xmin = max(xmin-(int16_t)p->scale-2, 0); xmax = min(xmax+(int16_t)p->scale+2, graph_get_XRes());
417
        ymin = max(ymin-(int16_t)p->scale-2, 0); ymax = min(ymax+(int16_t)p->scale+2, graph_get_YRes());
418
    }
419
    const uint8_t *map = basic_sprite_get_map(p->bsp);
420
    const uint16_t bytes_pixel = 3/*graph_get_bytes_pixel()*/;
421
    for(int16_t u, v, y = ymin; y < ymax; ++y){
422
        uint8_t *place = video_buf + (xmin + y*graph_get_XRes())*bytes_pixel;
423
        for(int16_t x = xmin; x < xmax; ++x, place += bytes_pixel){
424
            sprite_src2pic(p, x, y, &u, &v);
425
            if(0 <= u && u < w && 0 <= v && v < h){
426
                const uint8_t *c_p = map+(v*w+u)*4;
427
                if(*(c_p+3) < ALPHA_THRESHOLD) //alpha
428
                    memcpy(place, c_p, bytes_pixel);
429
            }
430
        }
431
    }
432
}
433 0

  
proj/src/fast_math.c
1
#include <lcom/lcf.h>
2

  
3
#include "fast_math.h"
4
#include "utils.h"
5

  
6
#include <math.h>
7

  
8
double fm_sin(double x){
9
    if(x < 0.0)         return -fm_sin(-x);
10
    if(x > 2.0*M_PI) return fm_sin(x-2.0*M_PI);
11
    if(x > M_PI)      return -fm_sin(x-M_PI);
12
    if(x > 0.5*M_PI) x = M_PI - x;
13
    double x2 = x*x;
14
    double x3 = x*x2;
15
    double x5 = x3*x2;
16
    //double x7 = x5*x2;
17
    return x-x3*0.1666666666666666666666+x5*0.008333333333333333333333;//-x7*0.0001984126984127;
18
}
19

  
20
double fm_cos(double x){
21
    if(x < 0.0)         x = -x;
22
    if(x > 2.0*M_PI) return fm_cos(x-2.0*M_PI);
23
    if(x > M_PI)      x = 2.0*M_PI-x;
24
    if(x > 0.5*M_PI) return -fm_cos(M_PI-x);
25
    double x2 = x*x;
26
    double x4 = x2*x2;
27
    double x6 = x4*x2;
28
    //double x8 = x4*x4;
29
    return 1.0-x2*0.5+x4*0.041666666666666666666666-x6*0.0013888888888888888888888;//+x8*0.000024801587;
30
}
31 0

  
proj/src/xpm_utils.c
1
#ifdef LCOM_MACRO
2
    #include <lcom/lcf.h>
3
#endif
4

  
5
#include "xpm_utils.h"
6

  
7
#include <stdio.h>
8
#include <stdlib.h>
9
#include <string.h>
10

  
11
void xpm_save_as_xpm2(const char **p, const char *s){
12
    FILE *f = fopen(s, "w");
13
    int w, h, num_colors, chars_per_pixel;
14
    sscanf(p[0], "%d %d %d %d", &w, &h, &num_colors, &chars_per_pixel);
15
    for(int i = 0; i < 1+num_colors+h; ++i){
16
        fprintf(f, "%s\n", p[i]);
17
    }
18
    fclose(f); f = NULL;
19
}
20

  
21
char** xpm_load_xpm2(const char *fpath){
22
    FILE *f = fopen(fpath, "r");
23
    if(f == NULL) return NULL;
24
    size_t len = 1024; char *line_buf = malloc(len*sizeof(char));
25
    int sz;
26

  
27
    char **ret = NULL;
28

  
29
    int w, h, num_colors, chars_per_pixel;
30

  
31
    sz = getline(&line_buf, &len, f);{
32
        sscanf(line_buf, "%d %d %d %d", &w, &h, &num_colors, &chars_per_pixel);
33
        ret = malloc((1+num_colors+h)*sizeof(char*));
34
    }
35
    ret[0] = malloc((sz+1)*sizeof(char)); if(ret[0] == NULL){ free(ret); return NULL; }
36
    strcpy(ret[0], line_buf);
37

  
38
    for(int i = 1; i < 1+num_colors+h; ++i){
39
        sz = getline(&line_buf, &len, f);
40
        ret[i] = malloc((sz+1)*sizeof(char));
41
        if(ret[i] == NULL){
42
            for(int j = 0; j < i; ++j)
43
                free(ret[i]);
44
            free(ret);
45
            return NULL;
46
        }
47
        strcpy(ret[i], line_buf);
48
        ret[i][sz-1] = '\0';
49
    }
50
    fclose(f); f = NULL;
51
    return ret;
52
}
53 0

  
proj/src/utils.c
1
#include <lcom/lcf.h>
2

  
3
#include "utils.h"
4

  
5
#include <stdint.h>
6
#include "errors.h"
7

  
8
int(util_get_LSB)(uint16_t val, uint8_t *lsb) {
9
    if (lsb == NULL) return NULL_PTR;
10
    *lsb = val;
11
    return SUCCESS;
12
}
13

  
14
int(util_get_MSB)(uint16_t val, uint8_t *msb) {
15
    if (msb == NULL) return NULL_PTR;
16
    *msb = (val >> 8);
17
    return SUCCESS;
18
}
19

  
20
int (util_sys_inb)(int port, uint8_t *value) {
21
    if(value == NULL) return NULL_PTR;
22
    uint32_t n = 0;
23
    if(sys_inb(port, &n)) return READ_ERROR;
24
    *value = n;
25
    return SUCCESS;
26
}
27

  
28
int (unsubscribe_interrupt)(int *interrupt_id) {
29
    if (interrupt_id == NULL) return NULL_PTR;
30
    if(sys_irqrmpolicy(interrupt_id)) return UNSBCR_ERROR;
31
    return SUCCESS;
32
}
33

  
34
int32_t min(int32_t a, int32_t b){ return (b < a ? b : a); }
35
int32_t max(int32_t a, int32_t b){ return (a < b ? b : a); }
36

  
37
double min_d(double a, double b){ return (b < a ? b : a); }
38
double max_d(double a, double b){ return (a < b ? b : a); }
39
double abs_d(double a)          { return (a < 0 ? -a: a); }
40 0

  
proj/libs/peripherals/include/graph.h
1
#ifndef GRAPH_H_INCLUDED
2
#define GRAPH_H_INCLUDED
3

  
4
// Graphics modes
5
#define INDEXED_1024_768        0x105
6
#define DIRECT_640_480_888      0x110
7
#define DIRECT_800_600_888      0x115
8
#define DIRECT_1024_768_888     0x118
9
#define DIRECT_1280_1024_565    0x11A
10
#define DIRECT_1280_1024_888    0x11B
11
#define LINEAR_FRAME_BUFFER_MD  BIT(14)
12

  
13
// Colors in RBG (8 bit)
14
#define GRAPH_BLACK               0x000000
15
#define GRAPH_WHITE               0xFFFFFF
16

  
17
// Alpha
18
#define ALPHA_THRESHOLD     0x7F
19

  
20
/// MACROS
21
#define GET_ALP(n)          (0xFF & ((n) >> 24))
22
#define GET_RED(n)          (0xFF & ((n) >> 16))
23
#define GET_GRE(n)          (0xFF & ((n) >>  8))
24
#define GET_BLU(n)          (0xFF & (n      ))
25
#define GET_COLOR(n)        (0xFFFFFF & (n))
26
#define SET_ALP(n)          (((n)&0xFF) << 24)
27
#define SET_RED(n)          (((n)&0xFF) << 16)
28
#define SET_GRE(n)          (((n)&0xFF) <<  8)
29
#define SET_BLU(n)          (((n)&0xFF)      )
30
#define SET_RGB(r,g,b)      (SET_RED(r) | SET_GRE(g) | SET_BLU(b))
31

  
32
/// PUBLIC GET
33
uint16_t   (graph_get_XRes)         (void);
34
uint16_t   (graph_get_YRes)         (void);
35
uint16_t   (graph_get_bytes_pixel)  (void);
36

  
37
/// INIT
38
int (graph_init)(uint16_t mode);
39

  
40
/// CLEANUP
41
int (graph_cleanup)(void);
42

  
43
/// PIXEL DRAWING
44
int (graph_set_pixel)             (uint16_t x, uint16_t y, uint32_t color);
45
void (graph_set_pixel_pos)         (unsigned pos          , uint32_t color);
46

  
47
/// SCREEN
48
int (graph_clear_screen)(void);
49

  
50
/// DRAW
51
int (graph_draw)(void);
52

  
53
#endif /* end of include guard: GRAPH_H_INCLUDED */
0 54

  
proj/libs/peripherals/src/graph.c
1
#include <lcom/lcf.h>
2

  
3
#include "graph.h"
4

  
5
#include "errors.h"
6
#include <stdio.h>
7

  
8
#define VC_BIOS_SERV  0x10 /** @brief Interrupt service of video card */
9
#define VBE_CALL      0x4F /** @brief VBE call function specifier */
10

  
11
#define MBYTE_BASE  0x0         /** @brief Base address (zero address) */
12
#define MBYTE_SIZE  0xFFFFF     /** @brief Size of a mebibyte */
13

  
14
// Graphics Functions
15
#define VBE_CTRL_INFO       0x00    /** @brief Get VBE Controller Information */
16
#define VBE_MD_INFO         0x01    /** @brief Get VBE Mode Information */
17
#define SET_VBE_MD          0x02    /** @brief Set VBE Mode */
18

  
19
// Error codes (AH)
20
#define AH_SUCCESS          0x00    /** @brief Success code on BIOS call */
21
#define AH_FUNC_CALL_FAIL   0x01    /** @brief Function call failed */
22
#define AH_FUNC_NOT_SUPP    0x02    /** @brief Function call is not supported in current HW configuration */
23
#define AH_FUNC_INVALID     0x03    /** @brief Invalid function in current video mode */
24

  
25
/// MACROS
26
#define FAR2PHYS(n)         ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF))
27

  
28
/// STRUCT
29
typedef struct __attribute__((packed)) {
30

  
31
    char        VbeSignature[4]     ;
32
    uint16_t    VbeVersion          ;
33
    uint32_t    OemStringPtr        ;
34
    uint8_t     Capabilities[4]     ;
35
    uint32_t    VideoModePtr        ;
36
    uint16_t    TotalMemory         ;
37

  
38
    uint16_t    OemSoftwareRev      ;
39
    uint32_t    OemVendorNamePtr    ;
40
    uint32_t    OemProductNamePtr   ;
41
    uint32_t    OemProductRevPtr    ;
42
    char        Reserved[222]       ;
43

  
44
    char        OemData[256]        ;
45
} VbeInfoBlock;
46

  
47
static vbe_mode_info_t vbe_mem_info;
48

  
49
/// PRIVATE GET
50
static uint16_t   (graph_get_bits_pixel)   (void){ return vbe_mem_info.BitsPerPixel; }
51
static phys_bytes (graph_get_phys_addr)    (void){ return vbe_mem_info.PhysBasePtr; }
52
static unsigned   (graph_get_vram_size)    (void){ return vbe_mem_info.XResolution * vbe_mem_info.YResolution * graph_get_bytes_pixel(); }
53
//static uint16_t   (graph_get_RedMaskSize)  (void){ return vbe_mem_info.RedMaskSize  ; }
54
//static uint16_t   (graph_get_GreenMaskSize)(void){ return vbe_mem_info.GreenMaskSize; }
55
//static uint16_t   (graph_get_BlueMaskSize) (void){ return vbe_mem_info.BlueMaskSize ; }
56

  
57
/// PUBLIC GET
58
uint16_t   (graph_get_XRes)         (void){ return vbe_mem_info.XResolution; }
59
uint16_t   (graph_get_YRes)         (void){ return vbe_mem_info.YResolution; }
60
uint16_t   (graph_get_bytes_pixel)  (void){ return (graph_get_bits_pixel() + 7) >> 3; }
61

  
62
///
63
static int (get_permission)(unsigned int base_addr, unsigned int size) {
64
    struct minix_mem_range mmr;
65
    mmr.mr_base = base_addr;
66
    mmr.mr_limit = base_addr + size;
67
    return sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr);
68
}
69

  
70
//static int (get_permissions_first_mbyte)(void) {
71
//    return get_permission(MBYTE_BASE, MBYTE_SIZE);
72
//}
73

  
74
/// MEMORY
75
static uint8_t *video_mem = NULL; /** @brief Frame-buffer VM address. */
76
static uint8_t *video_buf = NULL; /** @brief Primary buffer for drawing before copying to video_mem. */
77
static mmap_t mem_map;
78
static int (graph_free_memory)(void) {
79
    int r = SUCCESS;
80
    free(video_buf); video_buf = NULL;
81
    r = !lm_free(&mem_map);
82
    return r;
83
}
84
static int (graph_map_vram)(void) {
85
    int r;
86
    const unsigned vram_base = graph_get_phys_addr();
87
    const unsigned vram_size = graph_get_vram_size();
88
    if ((r = get_permission(vram_base, vram_size))) {
89
        if (graph_free_memory()) {
90
            printf("%s: lm_free failed\n", __func__);
91
        }
92
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
93
    }
94

  
95
    video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
96

  
97
    if (video_mem == MAP_FAILED) {
98
        if (graph_free_memory()) {
99
            printf("%s: lm_free failed\n", __func__);
100
        }
101
        panic("%s: couldn't map video memory.", __func__);
102
    }
103

  
104
    video_buf = malloc(vram_size);
105

  
106
    return SUCCESS;
107
}
108

  
109
/// INFO GET
110
static int (vbe_get_mode_information)(uint16_t mode) {
111
    memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // reset values
112

  
113
    struct reg86 reg_86;
114
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
115

  
116
    vbe_mode_info_t *virtual_addr = lm_alloc(sizeof(vbe_mode_info_t), &mem_map);
117

  
118
    reg_86.intno = VC_BIOS_SERV;
119
    reg_86.ah = VBE_CALL;
120
    reg_86.al = VBE_MD_INFO;
121
    reg_86.cx = mode;
122
    reg_86.es = PB2BASE(mem_map.phys);
123
    reg_86.di = PB2OFF(mem_map.phys);
124
    // BIOS CALL
125
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
126
        printf("%s: sys_int86 failed\n", __func__);
127
        if (graph_free_memory()) {
128
            printf("%s: lm_free failed\n", __func__);
129
        }
130
        return BIOS_CALL_ERROR;
131
    }
132

  
133
    memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size);
134
    return SUCCESS;
135
}
136
/*
137
static int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) {
138
    memset(info_p, 0, sizeof(vg_vbe_contr_info_t)); // reset values
139

  
140
    mmap_t controller_map;
141

  
142
    struct reg86 reg_86;
143
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
144

  
145
    VbeInfoBlock *virtual_addr = lm_alloc(sizeof(VbeInfoBlock), &controller_map);
146

  
147
    uint32_t virtual_base = (uint32_t)(virtual_addr) - controller_map.phys;
148

  
149
    virtual_addr->VbeSignature[0] = 'V';
150
    virtual_addr->VbeSignature[1] = 'B';
151
    virtual_addr->VbeSignature[2] = 'E';
152
    virtual_addr->VbeSignature[3] = '2';
153

  
154

  
155
    reg_86.intno = VC_BIOS_SERV;
156
    reg_86.ah = VBE_CALL;
157
    reg_86.al = VBE_CTRL_INFO;
158
    reg_86.es = PB2BASE(controller_map.phys);
159
    reg_86.di = PB2OFF(controller_map.phys);
160
    // BIOS CALL
161
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
162
        printf("%s: sys_int86 failed\n", __func__);
163
        if (!lm_free(&controller_map)) {
164
            printf("%s: lm_free failed\n", __func__);
165
        }
166
        return BIOS_CALL_ERROR;
167
    }
168

  
169
    info_p->VBESignature[0] = virtual_addr->VbeSignature[0];
170
    info_p->VBESignature[1] = virtual_addr->VbeSignature[1];
171
    info_p->VBESignature[2] = virtual_addr->VbeSignature[2];
172
    info_p->VBESignature[3] = virtual_addr->VbeSignature[3];
173

  
174
    uint8_t lsb, msb;
175
    util_get_LSB(virtual_addr->VbeVersion, &lsb);
176
    util_get_MSB(virtual_addr->VbeVersion, &msb);
177
    info_p->VBEVersion[0] = lsb;
178
    info_p->VBEVersion[1] = msb;
179

  
180
    info_p->TotalMemory = (virtual_addr->TotalMemory << 6);
181

  
182
    // Convert Far Far Pointer to Virtual Address
183

  
184
    uint32_t phys_ptr = FAR2PHYS(virtual_addr->OemStringPtr);
185
    uint32_t virtual_ptr = phys_ptr + virtual_base;
186
    info_p->OEMString = (char*)(virtual_ptr);
187

  
188
    phys_ptr = FAR2PHYS(virtual_addr->VideoModePtr);
189
    virtual_ptr = phys_ptr + virtual_base;
190
    info_p->VideoModeList = (uint16_t*)(virtual_ptr);
191

  
192
    phys_ptr = FAR2PHYS(virtual_addr->OemVendorNamePtr);
193
    virtual_ptr = phys_ptr + virtual_base;
194
    info_p->OEMVendorNamePtr = (char*)(virtual_ptr);
195

  
196
    phys_ptr = FAR2PHYS(virtual_addr->OemProductNamePtr);
197
    virtual_ptr = phys_ptr + virtual_base;
198
    info_p->OEMProductNamePtr = (char*)(virtual_ptr);
199

  
200
    phys_ptr = FAR2PHYS(virtual_addr->OemProductRevPtr);
201
    virtual_ptr = phys_ptr + virtual_base;
202
    info_p->OEMProductRevPtr = (char*)(virtual_ptr);
203

  
204
    if (!lm_free(&controller_map)) {
205
        printf("%s: lm_free failed\n", __func__);
206
        return LCF_ERROR;
207
    }
208

  
209
    return SUCCESS;
210
}
211
*/
212

  
213
/// INIT
214
/**
215
 * @brief
216
 * @param mode
217
 * @return
218
 */
219
static int (graph_set_mode)(uint16_t mode) {
220
    struct reg86 reg_86;
221

  
222
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
223

  
224
    // Set Reg86
225
    reg_86.intno = VC_BIOS_SERV;
226
    reg_86.ah = VBE_CALL;
227
    reg_86.al = SET_VBE_MD;
228
    reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD;
229

  
230
    // BIOS CALL
231
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
232
        printf("%s: sys_int86 failed\n", __func__);
233
        return BIOS_CALL_ERROR;
234
    }
235

  
236
    return SUCCESS;
237
}
238
int (graph_init)(uint16_t mode){
239
    if (vbe_get_mode_information(mode)) {
240
        printf("%s: failed to get information for mode %x.\n", __func__, mode);
241
        return 1;
242
    }
243

  
244
    graph_map_vram(); // if function fails it aborts program
245

  
246
    if (graph_set_mode(mode)) {
247
        printf("%s: failed to set graphic mode %x.\n", __func__, mode);
248
        return 1;
249
    };
250
    return SUCCESS;
251
}
252

  
253
/// CLEANUP
254
int (graph_cleanup)(void){
255
    int r = SUCCESS;
256
    if ((r = vg_exit()))
257
        printf("%s: vg_exit failed to exit to text mode.\n", __func__);
258
    if ((r = graph_free_memory()))
259
        printf("%s: lm_free failed\n", __func__);
260
    return r;
261
}
262

  
263
/// PIXEL DRAWING
264
int (graph_set_pixel)(uint16_t x, uint16_t y, uint32_t color) {
265
    //pixels are certain to be inside can reduce lag
266
    /*if (x < 0 || vbe_mem_info.XResolution <= x || y < 0 || vbe_mem_info.YResolution <= y) {
267
        //printf("%s: invalid pixel.\n", __func__);
268
        return OUT_OF_RANGE;
269
    }*/
270
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * 3/*graph_get_bytes_pixel()*/;
271
    memcpy(video_buf + pos, &color, 3/*graph_get_bytes_pixel()*/);
272
    return SUCCESS;
273
}
274
void (graph_set_pixel_pos)(unsigned pos, uint32_t color){
275
    memcpy(video_buf + pos, &color, graph_get_bytes_pixel());
276
}
277
int (graph_clear_screen)(void){ memset(video_buf, 0, graph_get_vram_size()); return SUCCESS; }
278
int (graph_draw)(void){ memcpy(video_mem, video_buf, graph_get_vram_size()); return SUCCESS; }
279

  
280
///SPRITE
281
#include "sprite.h"
282

  
283
#include "utils.h"
284
#include "fast_math.h"
285
#include <math.h>
286

  
287
struct basic_sprite{
288
    uint8_t *map;
289
    uint16_t w, h;
290
    int16_t u0, v0;
291
};
292
basic_sprite_t* (basic_sprite_ctor)(const char **xpm, int16_t u0, int16_t v0){
293
    basic_sprite_t *ret = malloc(sizeof(basic_sprite_t));
294
    if(ret == NULL) return NULL;
295
    enum xpm_image_type type = XPM_8_8_8_8;
296
    xpm_image_t img;
297
    ret->map = xpm_load((xpm_map_t)xpm, type, &img);
298
    if(ret->map == NULL){
299
        basic_sprite_dtor(ret);
300
        return NULL;
301
    }
302
    ret->w = img.width;
303
    ret->h = img.height;
304
    ret->u0 = u0;
305
    ret->v0 = v0;
306
    return ret;
307
}
308
void (basic_sprite_dtor)(basic_sprite_t *p){
309
    if(p == NULL) return;
310
    free(p->map);
311
    free(p);
312
}
313
const uint8_t* (basic_sprite_get_map)(const basic_sprite_t *p){ return p->map; }
314
uint16_t       (basic_sprite_get_w)  (const basic_sprite_t *p){ return p->w  ; }
315
uint16_t       (basic_sprite_get_h)  (const basic_sprite_t *p){ return p->h  ; }
316
int16_t        (basic_sprite_get_u0) (const basic_sprite_t *p){ return p->u0 ; }
317
int16_t        (basic_sprite_get_v0) (const basic_sprite_t *p){ return p->v0 ; }
318

  
319
/*
320
struct basic_sprite_alpha{
321
    uint8_t *map;
322
    uint16_t w, h;
323
    int16_t u0, v0;
324
};
325
basic_sprite_alpha_t* (basic_sprite_alpha_ctor)(const char **xpm, int16_t u0, int16_t v0){
326
    basic_sprite_alpha_t *ret = malloc(sizeof(basic_sprite_t));
327
    if(ret == NULL) return NULL;
328
    enum xpm_image_type type = XPM_8_8_8_8;
329
    xpm_image_t img;
330
    ret->map = NULL;
331
    uint8_t *m = xpm_load((xpm_map_t)xpm, type, &img);
332
    if(m == NULL){
333
        basic_sprite_alpha_dtor(ret);
334
        return NULL;
335
    }
336
    ret->map = m;
337
    if(ret->map == NULL){
338
        basic_sprite_alpha_dtor(ret);
339
        return NULL;
340
    }
341
    ret->w = img.width;
342
    ret->h = img.height;
343
    ret->u0 = u0;
344
    ret->v0 = v0;
345
    return ret;
346
}
347
void (basic_sprite_alpha_dtor)(basic_sprite_alpha_t *p){
348
    if(p == NULL) return;
349
    free(p->map);
350
    free(p);
351
}
352
const uint8_t* (basic_sprite_alpha_get_map)(const basic_sprite_alpha_t *p){ return p->map; }
353
uint16_t       (basic_sprite_alpha_get_w)  (const basic_sprite_alpha_t *p){ return p->w  ; }
354
uint16_t       (basic_sprite_alpha_get_h)  (const basic_sprite_alpha_t *p){ return p->h  ; }
355
int16_t        (basic_sprite_alpha_get_u0) (const basic_sprite_alpha_t *p){ return p->u0 ; }
356
int16_t        (basic_sprite_alpha_get_v0) (const basic_sprite_alpha_t *p){ return p->v0 ; }
357
*/
358

  
359
struct sprite{
360
    const basic_sprite_t *bsp;
361
    int16_t x, y; //position in screen
362
    double theta, s, c;
363
    double scale;
364
};
365
sprite_t* (sprite_ctor)(const basic_sprite_t *bsp){
366
    sprite_t *ret = malloc(sizeof(sprite_t));
367
    if(ret == NULL) return NULL;
368
    ret->bsp = bsp;
369
    ret->x = 0;
370
    ret->y = 0;
371
    sprite_set_angle(ret, 0.0);
372
    ret->scale = 1.0;
373
    return ret;
374
}
375
void (sprite_dtor)(sprite_t *p){
376
    if(p == NULL) return;
377
    free(p);
378
}
379
void (sprite_set_pos)   (sprite_t *p, int16_t x , int16_t y ){ p->x = x; p->y = y; }
380
void (sprite_set_angle) (sprite_t *p, double angle          ){ p->theta = angle; p->c = fm_cos(p->theta); p->s = fm_sin(p->theta); }
381
void (sprite_set_scale) (sprite_t *p, double scale          ){ p->scale = scale; }
382
int16_t  (sprite_get_x)(const sprite_t *p){ return p->x; }
383
int16_t  (sprite_get_y)(const sprite_t *p){ return p->y; }
384
double   (sprite_get_angle)(const sprite_t *p){ return p->theta; }
385
uint16_t (sprite_get_w)(const sprite_t *p){ return basic_sprite_get_w(p->bsp); }
386
uint16_t (sprite_get_h)(const sprite_t *p){ return basic_sprite_get_h(p->bsp); }
387
void (sprite_src2pic)(const sprite_t *p, int16_t x, int16_t y, int16_t *u, int16_t *v){
388
    double dx = (x - p->x)/p->scale;
389
    double dy = (y - p->y)/p->scale;
390
    int16_t du = dx*p->c - dy*p->s - 0.5;
391
    int16_t dv = dx*p->s + dy*p->c - 0.5;
392
    *u = du + basic_sprite_get_u0(p->bsp);
393
    *v = dv + basic_sprite_get_v0(p->bsp);
394
}
395
void (sprite_pic2src)(const sprite_t *p, int16_t u, int16_t v, int16_t *x, int16_t *y){
396
    int16_t du = u - basic_sprite_get_u0(p->bsp);
397
    int16_t dv = v - basic_sprite_get_v0(p->bsp);
398
    double dx =  du*p->c + dv*p->s;
399
    double dy = -du*p->s + dv*p->c;
400
    *x = dx*p->scale + 0.5 + p->x;
401
    *y = dy*p->scale + 0.5 + p->y;
402
}
403
void (sprite_draw)(const sprite_t *p){
404
    const uint16_t w = basic_sprite_get_w(p->bsp);
405
    const uint16_t h = basic_sprite_get_h(p->bsp);
406
    int16_t xmin, xmax, ymin, ymax; {
407
        int16_t x, y;
408
        sprite_pic2src(p, 0, 0, &x, &y);
409
        xmin = x; xmax = x; ymin = y; ymax = y;
410
        sprite_pic2src(p, w, 0, &x, &y);
411
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
412
        sprite_pic2src(p, 0, h, &x, &y);
413
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
414
        sprite_pic2src(p, w, h, &x, &y);
415
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
416
        xmin = max(xmin-(int16_t)p->scale-2, 0); xmax = min(xmax+(int16_t)p->scale+2, graph_get_XRes());
417
        ymin = max(ymin-(int16_t)p->scale-2, 0); ymax = min(ymax+(int16_t)p->scale+2, graph_get_YRes());
418
    }
419
    const uint8_t *map = basic_sprite_get_map(p->bsp);
420
    const uint16_t bytes_pixel = 3/*graph_get_bytes_pixel()*/;
421
    for(int16_t u, v, y = ymin; y < ymax; ++y){
422
        uint8_t *place = video_buf + (xmin + y*graph_get_XRes())*bytes_pixel;
423
        for(int16_t x = xmin; x < xmax; ++x, place += bytes_pixel){
424
            sprite_src2pic(p, x, y, &u, &v);
425
            if(0 <= u && u < w && 0 <= v && v < h){
426
                const uint8_t *c_p = map+(v*w+u)*4;
427
                if(*(c_p+3) < ALPHA_THRESHOLD) //alpha
428
                    memcpy(place, c_p, bytes_pixel);
429
            }
430
        }
431
    }
432
}
0 433

  
proj/libs/utils/include/fast_math.h
1
#ifndef FAST_MATH_H_INCLUDED
2
#define FAST_MATH_H_INCLUDED
3

  
4
double fm_sin(double x);
5
double fm_cos(double x);
6

  
7
#endif //FAST_MATH_H_INCLUDED
0 8

  
proj/libs/utils/include/utils.h
1
#ifndef UTILS_H_INCLUDED
2
#define UTILS_H_INCLUDED
3

  
4
#define BCD_FIRST(n)     (n >> 4)       /** @brief Get first digit (leftmost digit) of 8-bit BCD */
5
#define BCD_SECOND(n)    (n & 0x0F)     /** @brief Get second digit (rightmost digit) of 8-bit BCD */
6

  
7
/**
8
 * @brief Gets the least significant byte of a 16-bit variable
9
 * @param val 16-bit variable
10
 * @param lsb Pointer to a 8-bit variable to store the value of the LSB
11
 * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
12
 */
13
int(util_get_LSB)(uint16_t val, uint8_t *lsb);
14

  
15
/**
16
 * @brief Gets the most significant byte of a 16-bit variable
17
 * @param val 16-bit variable
18
 * @param lsb Pointer to a 8-bit variable to store the value of the MSB
19
 * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
20
 */
21
int(util_get_MSB)(uint16_t val, uint8_t *msb);
22

  
23
/**
24
 * @brief sys_inb wrapper
25
 * @param port Port to read from
26
 * @param value Pointer to byte to store value read
27
 * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
28
 */
29
int (util_sys_inb)(int port, uint8_t *value);
30

  
31
/**
32
 * @brief Unsubcribes Interrupts
33
 * @param interrupt_id Interrupt ID, value via arguments on subscription of the interrupt_id
34
 * @see subscribe_kbc_interrupt, subscribe_timer_interrupt
35
 * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
36
 */
37
int (unsubscribe_interrupt)(int *interrupt_id);
38

  
39
/**
40
 * @brief Gets the minimum value out of two values.
41
 * @param a     First value
42
 * @param b     Second value
43
 * @return  The minimum of the two values
44
 */
45
int32_t min(int32_t a, int32_t b);
46

  
47
/**
48
 * @brief Gets the maximum value out of two values.
49
 * @param a     First value
50
 * @param b     Second value
51
 * @return  The maximum of the two values
52
 */
53
int32_t max(int32_t a, int32_t b);
54

  
55
/**
56
 * @brief Gets the minimum value out of two doubles.
57
 * @param a     First value
58
 * @param b     Second value
59
 * @return  The minimum of the two values
60
 */
61
double min_d(double a, double b);
62

  
63
/**
64
 * @brief Gets the maximum value out of two doubles.
65
 * @param a     First value
66
 * @param b     Second value
67
 * @return  The maximum of the two values
68
 */
69
double max_d(double a, double b);
70

  
71

  
72
double abs_d(double a);
73

  
74

  
75
#endif //UTILS_H_INCLUDED
0 76

  
proj/libs/utils/include/xpm_utils.h
1
#ifndef XMP_UTILS_H_INCLUDED
2
#define XMP_UTILS_H_INCLUDED
3

  
4
void xpm_save_as_xpm2(const char **p, const char *s);
5

  
6
char** xpm_load_xpm2(const char *s);
7

  
8
#endif //XMP_UTILS_H_INCLUDED
0 9

  
proj/libs/utils/src/fast_math.c
1
#include <lcom/lcf.h>
2

  
3
#include "fast_math.h"
4
#include "utils.h"
5

  
6
#include <math.h>
7

  
8
double fm_sin(double x){
9
    if(x < 0.0)         return -fm_sin(-x);
10
    if(x > 2.0*M_PI) return fm_sin(x-2.0*M_PI);
11
    if(x > M_PI)      return -fm_sin(x-M_PI);
12
    if(x > 0.5*M_PI) x = M_PI - x;
13
    double x2 = x*x;
14
    double x3 = x*x2;
15
    double x5 = x3*x2;
16
    //double x7 = x5*x2;
17
    return x-x3*0.1666666666666666666666+x5*0.008333333333333333333333;//-x7*0.0001984126984127;
18
}
19

  
20
double fm_cos(double x){
21
    if(x < 0.0)         x = -x;
22
    if(x > 2.0*M_PI) return fm_cos(x-2.0*M_PI);
23
    if(x > M_PI)      x = 2.0*M_PI-x;
24
    if(x > 0.5*M_PI) return -fm_cos(M_PI-x);
25
    double x2 = x*x;
26
    double x4 = x2*x2;
27
    double x6 = x4*x2;
28
    //double x8 = x4*x4;
29
    return 1.0-x2*0.5+x4*0.041666666666666666666666-x6*0.0013888888888888888888888;//+x8*0.000024801587;
30
}
0 31

  
proj/libs/utils/src/utils.c
1
#include <lcom/lcf.h>
2

  
3
#include "utils.h"
4

  
5
#include <stdint.h>
6
#include "errors.h"
7

  
8
int(util_get_LSB)(uint16_t val, uint8_t *lsb) {
9
    if (lsb == NULL) return NULL_PTR;
10
    *lsb = val;
11
    return SUCCESS;
12
}
13

  
14
int(util_get_MSB)(uint16_t val, uint8_t *msb) {
15
    if (msb == NULL) return NULL_PTR;
16
    *msb = (val >> 8);
17
    return SUCCESS;
18
}
19

  
20
int (util_sys_inb)(int port, uint8_t *value) {
21
    if(value == NULL) return NULL_PTR;
22
    uint32_t n = 0;
23
    if(sys_inb(port, &n)) return READ_ERROR;
24
    *value = n;
25
    return SUCCESS;
26
}
27

  
28
int (unsubscribe_interrupt)(int *interrupt_id) {
29
    if (interrupt_id == NULL) return NULL_PTR;
30
    if(sys_irqrmpolicy(interrupt_id)) return UNSBCR_ERROR;
31
    return SUCCESS;
32
}
33

  
34
int32_t min(int32_t a, int32_t b){ return (b < a ? b : a); }
35
int32_t max(int32_t a, int32_t b){ return (a < b ? b : a); }
36

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff