Project

General

Profile

Statistics
| Revision:

root / proj / src / graph.c @ 254

History | View | Annotate | Download (15.3 KB)

1 178 up20180642
#include <lcom/lcf.h>
2
3 168 up20180642
#include "graph.h"
4 183 up20180642
5 168 up20180642
#include "errors.h"
6
#include <stdio.h>
7
8 253 up20180642
#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
// Graphics modes
26
#define INDEXED_1024_768        0x105
27
#define DIRECT_640_480_888      0x110
28
#define DIRECT_800_600_888      0x115
29
#define DIRECT_1024_768_888     0x118
30
#define DIRECT_1280_1024_565    0x11A
31
#define DIRECT_1280_1024_888    0x11B
32
#define LINEAR_FRAME_BUFFER_MD  BIT(14)
33
34
35
36
37
38 174 up20180642
/// MACROS
39
#define FAR2PHYS(n)         ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF))
40
41
/// STRUCT
42
typedef struct __attribute__((packed)) {
43
44
    char        VbeSignature[4]     ;
45
    uint16_t    VbeVersion          ;
46
    uint32_t    OemStringPtr        ;
47
    uint8_t     Capabilities[4]     ;
48
    uint32_t    VideoModePtr        ;
49
    uint16_t    TotalMemory         ;
50
51
    uint16_t    OemSoftwareRev      ;
52
    uint32_t    OemVendorNamePtr    ;
53
    uint32_t    OemProductNamePtr   ;
54
    uint32_t    OemProductRevPtr    ;
55
    char        Reserved[222]       ;
56
57
    char        OemData[256]        ;
58
} VbeInfoBlock;
59
60 168 up20180642
static vbe_mode_info_t vbe_mem_info;
61
62 174 up20180642
/// PRIVATE GET
63
static uint16_t   (graph_get_bits_pixel)   (void){ return vbe_mem_info.BitsPerPixel; }
64
static phys_bytes (graph_get_phys_addr)    (void){ return vbe_mem_info.PhysBasePtr; }
65
static unsigned   (graph_get_vram_size)    (void){ return vbe_mem_info.XResolution * vbe_mem_info.YResolution * graph_get_bytes_pixel(); }
66
//static uint16_t   (graph_get_RedMaskSize)  (void){ return vbe_mem_info.RedMaskSize  ; }
67
//static uint16_t   (graph_get_GreenMaskSize)(void){ return vbe_mem_info.GreenMaskSize; }
68
//static uint16_t   (graph_get_BlueMaskSize) (void){ return vbe_mem_info.BlueMaskSize ; }
69
70 208 up20180642
/// PUBLIC GET
71
uint16_t   (graph_get_XRes)         (void){ return vbe_mem_info.XResolution; }
72
uint16_t   (graph_get_YRes)         (void){ return vbe_mem_info.YResolution; }
73
uint16_t   (graph_get_bytes_pixel)  (void){ return (graph_get_bits_pixel() + 7) >> 3; }
74
75
///
76 174 up20180642
static int (get_permission)(unsigned int base_addr, unsigned int size) {
77 168 up20180642
    struct minix_mem_range mmr;
78
    mmr.mr_base = base_addr;
79
    mmr.mr_limit = base_addr + size;
80
    return sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr);
81
}
82
83 174 up20180642
//static int (get_permissions_first_mbyte)(void) {
84 168 up20180642
//    return get_permission(MBYTE_BASE, MBYTE_SIZE);
85
//}
86
87 174 up20180642
/// MEMORY
88 178 up20180642
static uint8_t *video_mem = NULL; /** @brief Frame-buffer VM address. */
89 174 up20180642
static uint8_t *video_buf = NULL; /** @brief Primary buffer for drawing before copying to video_mem. */
90
static mmap_t mem_map;
91
static int (graph_free_memory)(void) {
92
    int r = SUCCESS;
93
    free(video_buf); video_buf = NULL;
94
    r = !lm_free(&mem_map);
95
    return r;
96
}
97
static int (graph_map_vram)(void) {
98
    int r;
99
    const unsigned vram_base = graph_get_phys_addr();
100
    const unsigned vram_size = graph_get_vram_size();
101
    if ((r = get_permission(vram_base, vram_size))) {
102
        if (graph_free_memory()) {
103
            printf("%s: lm_free failed\n", __func__);
104
        }
105
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
106 168 up20180642
    }
107
108 174 up20180642
    video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
109 168 up20180642
110 174 up20180642
    if (video_mem == MAP_FAILED) {
111
        if (graph_free_memory()) {
112
            printf("%s: lm_free failed\n", __func__);
113
        }
114
        panic("%s: couldn't map video memory.", __func__);
115
    }
116
117
    video_buf = malloc(vram_size);
118
119 168 up20180642
    return SUCCESS;
120
}
121
122 174 up20180642
/// INFO GET
123
static int (vbe_get_mode_information)(uint16_t mode) {
124 168 up20180642
    memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // reset values
125
126
    struct reg86 reg_86;
127
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
128
129
    vbe_mode_info_t *virtual_addr = lm_alloc(sizeof(vbe_mode_info_t), &mem_map);
130
131
    reg_86.intno = VC_BIOS_SERV;
132
    reg_86.ah = VBE_CALL;
133
    reg_86.al = VBE_MD_INFO;
134
    reg_86.cx = mode;
135
    reg_86.es = PB2BASE(mem_map.phys);
136
    reg_86.di = PB2OFF(mem_map.phys);
137
    // BIOS CALL
138
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
139
        printf("%s: sys_int86 failed\n", __func__);
140
        if (graph_free_memory()) {
141
            printf("%s: lm_free failed\n", __func__);
142
        }
143
        return BIOS_CALL_ERROR;
144
    }
145
146
    memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size);
147
    return SUCCESS;
148
}
149 174 up20180642
/*
150
static int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) {
151 168 up20180642
    memset(info_p, 0, sizeof(vg_vbe_contr_info_t)); // reset values
152

153
    mmap_t controller_map;
154

155
    struct reg86 reg_86;
156
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
157

158
    VbeInfoBlock *virtual_addr = lm_alloc(sizeof(VbeInfoBlock), &controller_map);
159

160
    uint32_t virtual_base = (uint32_t)(virtual_addr) - controller_map.phys;
161

162
    virtual_addr->VbeSignature[0] = 'V';
163
    virtual_addr->VbeSignature[1] = 'B';
164
    virtual_addr->VbeSignature[2] = 'E';
165
    virtual_addr->VbeSignature[3] = '2';
166

167

168
    reg_86.intno = VC_BIOS_SERV;
169
    reg_86.ah = VBE_CALL;
170
    reg_86.al = VBE_CTRL_INFO;
171
    reg_86.es = PB2BASE(controller_map.phys);
172
    reg_86.di = PB2OFF(controller_map.phys);
173
    // BIOS CALL
174
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
175
        printf("%s: sys_int86 failed\n", __func__);
176
        if (!lm_free(&controller_map)) {
177
            printf("%s: lm_free failed\n", __func__);
178
        }
179
        return BIOS_CALL_ERROR;
180
    }
181

182
    info_p->VBESignature[0] = virtual_addr->VbeSignature[0];
183
    info_p->VBESignature[1] = virtual_addr->VbeSignature[1];
184
    info_p->VBESignature[2] = virtual_addr->VbeSignature[2];
185
    info_p->VBESignature[3] = virtual_addr->VbeSignature[3];
186

187
    uint8_t lsb, msb;
188
    util_get_LSB(virtual_addr->VbeVersion, &lsb);
189
    util_get_MSB(virtual_addr->VbeVersion, &msb);
190
    info_p->VBEVersion[0] = lsb;
191
    info_p->VBEVersion[1] = msb;
192

193
    info_p->TotalMemory = (virtual_addr->TotalMemory << 6);
194

195
    // Convert Far Far Pointer to Virtual Address
196

197
    uint32_t phys_ptr = FAR2PHYS(virtual_addr->OemStringPtr);
198
    uint32_t virtual_ptr = phys_ptr + virtual_base;
199
    info_p->OEMString = (char*)(virtual_ptr);
200

201
    phys_ptr = FAR2PHYS(virtual_addr->VideoModePtr);
202
    virtual_ptr = phys_ptr + virtual_base;
203
    info_p->VideoModeList = (uint16_t*)(virtual_ptr);
204

205
    phys_ptr = FAR2PHYS(virtual_addr->OemVendorNamePtr);
206
    virtual_ptr = phys_ptr + virtual_base;
207
    info_p->OEMVendorNamePtr = (char*)(virtual_ptr);
208

209
    phys_ptr = FAR2PHYS(virtual_addr->OemProductNamePtr);
210
    virtual_ptr = phys_ptr + virtual_base;
211
    info_p->OEMProductNamePtr = (char*)(virtual_ptr);
212

213
    phys_ptr = FAR2PHYS(virtual_addr->OemProductRevPtr);
214
    virtual_ptr = phys_ptr + virtual_base;
215
    info_p->OEMProductRevPtr = (char*)(virtual_ptr);
216

217
    if (!lm_free(&controller_map)) {
218
        printf("%s: lm_free failed\n", __func__);
219
        return LCF_ERROR;
220
    }
221

222
    return SUCCESS;
223
}
224 174 up20180642
*/
225 168 up20180642
226 174 up20180642
/// INIT
227
/**
228
 * @brief
229
 * @param mode
230
 * @return
231
 */
232
static int (graph_set_mode)(uint16_t mode) {
233 168 up20180642
    struct reg86 reg_86;
234
235
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
236
237
    // Set Reg86
238
    reg_86.intno = VC_BIOS_SERV;
239
    reg_86.ah = VBE_CALL;
240
    reg_86.al = SET_VBE_MD;
241
    reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD;
242
243
    // BIOS CALL
244
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
245
        printf("%s: sys_int86 failed\n", __func__);
246
        return BIOS_CALL_ERROR;
247
    }
248
249
    return SUCCESS;
250
}
251 174 up20180642
int (graph_init)(uint16_t mode){
252
    if (vbe_get_mode_information(mode)) {
253
        printf("%s: failed to get information for mode %x.\n", __func__, mode);
254
        return 1;
255
    }
256 168 up20180642
257 174 up20180642
    graph_map_vram(); // if function fails it aborts program
258
259
    if (graph_set_mode(mode)) {
260
        printf("%s: failed to set graphic mode %x.\n", __func__, mode);
261
        return 1;
262
    };
263
    return SUCCESS;
264
}
265
266
/// CLEANUP
267
int (graph_cleanup)(void){
268
    int r = SUCCESS;
269
    if ((r = vg_exit()))
270
        printf("%s: vg_exit failed to exit to text mode.\n", __func__);
271
    if ((r = graph_free_memory()))
272
        printf("%s: lm_free failed\n", __func__);
273
    return r;
274
}
275
276
/// PIXEL DRAWING
277 168 up20180642
int (graph_set_pixel)(uint16_t x, uint16_t y, uint32_t color) {
278 250 up20180655
    //pixels are certain to be inside can reduce lag
279
    /*if (x < 0 || vbe_mem_info.XResolution <= x || y < 0 || vbe_mem_info.YResolution <= y) {
280 173 up20180655
        //printf("%s: invalid pixel.\n", __func__);
281 168 up20180642
        return OUT_OF_RANGE;
282 250 up20180655
    }*/
283
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * 3/*graph_get_bytes_pixel()*/;
284
    memcpy(video_buf + pos, &color, 3/*graph_get_bytes_pixel()*/);
285 168 up20180642
    return SUCCESS;
286
}
287 208 up20180642
void (graph_set_pixel_pos)(unsigned pos, uint32_t color){
288 207 up20180642
    memcpy(video_buf + pos, &color, graph_get_bytes_pixel());
289 168 up20180642
}
290 208 up20180642
int (graph_clear_screen)(void){ memset(video_buf, 0, graph_get_vram_size()); return SUCCESS; }
291
int (graph_draw)(void){ memcpy(video_mem, video_buf, graph_get_vram_size()); return SUCCESS; }
292 174 up20180642
293 211 up20180642
///SPRITE
294 212 up20180642
#include "sprite.h"
295
296 211 up20180642
#include "utils.h"
297
#include "fast_math.h"
298
#include <math.h>
299
300
struct basic_sprite{
301
    uint8_t *map;
302
    uint16_t w, h;
303
    int16_t u0, v0;
304
};
305
basic_sprite_t* (basic_sprite_ctor)(const char **xpm, int16_t u0, int16_t v0){
306
    basic_sprite_t *ret = malloc(sizeof(basic_sprite_t));
307
    if(ret == NULL) return NULL;
308
    enum xpm_image_type type = XPM_8_8_8_8;
309
    xpm_image_t img;
310
    ret->map = xpm_load((xpm_map_t)xpm, type, &img);
311
    if(ret->map == NULL){
312 216 up20180642
        basic_sprite_dtor(ret);
313 211 up20180642
        return NULL;
314
    }
315
    ret->w = img.width;
316
    ret->h = img.height;
317
    ret->u0 = u0;
318
    ret->v0 = v0;
319
    return ret;
320
}
321
void (basic_sprite_dtor)(basic_sprite_t *p){
322
    if(p == NULL) return;
323
    free(p->map);
324
    free(p);
325
}
326
const uint8_t* (basic_sprite_get_map)(const basic_sprite_t *p){ return p->map; }
327
uint16_t       (basic_sprite_get_w)  (const basic_sprite_t *p){ return p->w  ; }
328
uint16_t       (basic_sprite_get_h)  (const basic_sprite_t *p){ return p->h  ; }
329
int16_t        (basic_sprite_get_u0) (const basic_sprite_t *p){ return p->u0 ; }
330
int16_t        (basic_sprite_get_v0) (const basic_sprite_t *p){ return p->v0 ; }
331
332 216 up20180642
/*
333
struct basic_sprite_alpha{
334
    uint8_t *map;
335
    uint16_t w, h;
336
    int16_t u0, v0;
337
};
338
basic_sprite_alpha_t* (basic_sprite_alpha_ctor)(const char **xpm, int16_t u0, int16_t v0){
339
    basic_sprite_alpha_t *ret = malloc(sizeof(basic_sprite_t));
340
    if(ret == NULL) return NULL;
341
    enum xpm_image_type type = XPM_8_8_8_8;
342
    xpm_image_t img;
343
    ret->map = NULL;
344
    uint8_t *m = xpm_load((xpm_map_t)xpm, type, &img);
345
    if(m == NULL){
346
        basic_sprite_alpha_dtor(ret);
347
        return NULL;
348
    }
349
    ret->map = m;
350
    if(ret->map == NULL){
351
        basic_sprite_alpha_dtor(ret);
352
        return NULL;
353
    }
354
    ret->w = img.width;
355
    ret->h = img.height;
356
    ret->u0 = u0;
357
    ret->v0 = v0;
358
    return ret;
359
}
360
void (basic_sprite_alpha_dtor)(basic_sprite_alpha_t *p){
361
    if(p == NULL) return;
362
    free(p->map);
363
    free(p);
364
}
365
const uint8_t* (basic_sprite_alpha_get_map)(const basic_sprite_alpha_t *p){ return p->map; }
366
uint16_t       (basic_sprite_alpha_get_w)  (const basic_sprite_alpha_t *p){ return p->w  ; }
367
uint16_t       (basic_sprite_alpha_get_h)  (const basic_sprite_alpha_t *p){ return p->h  ; }
368
int16_t        (basic_sprite_alpha_get_u0) (const basic_sprite_alpha_t *p){ return p->u0 ; }
369
int16_t        (basic_sprite_alpha_get_v0) (const basic_sprite_alpha_t *p){ return p->v0 ; }
370
*/
371
372 211 up20180642
struct sprite{
373
    const basic_sprite_t *bsp;
374
    int16_t x, y; //position in screen
375
    double theta, s, c;
376
    double scale;
377
};
378
sprite_t* (sprite_ctor)(const basic_sprite_t *bsp){
379
    sprite_t *ret = malloc(sizeof(sprite_t));
380
    if(ret == NULL) return NULL;
381
    ret->bsp = bsp;
382
    ret->x = 0;
383
    ret->y = 0;
384
    sprite_set_angle(ret, 0.0);
385
    ret->scale = 1.0;
386
    return ret;
387
}
388
void (sprite_dtor)(sprite_t *p){
389
    if(p == NULL) return;
390
    free(p);
391
}
392
void (sprite_set_pos)   (sprite_t *p, int16_t x , int16_t y ){ p->x = x; p->y = y; }
393
void (sprite_set_angle) (sprite_t *p, double angle          ){ p->theta = angle; p->c = fm_cos(p->theta); p->s = fm_sin(p->theta); }
394
void (sprite_set_scale) (sprite_t *p, double scale          ){ p->scale = scale; }
395
int16_t  (sprite_get_x)(const sprite_t *p){ return p->x; }
396
int16_t  (sprite_get_y)(const sprite_t *p){ return p->y; }
397 231 up20180655
double   (sprite_get_angle)(const sprite_t *p){ return p->theta; }
398 216 up20180642
uint16_t (sprite_get_w)(const sprite_t *p){ return basic_sprite_get_w(p->bsp); }
399
uint16_t (sprite_get_h)(const sprite_t *p){ return basic_sprite_get_h(p->bsp); }
400 211 up20180642
void (sprite_src2pic)(const sprite_t *p, int16_t x, int16_t y, int16_t *u, int16_t *v){
401
    double dx = (x - p->x)/p->scale;
402
    double dy = (y - p->y)/p->scale;
403 215 up20180642
    int16_t du = dx*p->c - dy*p->s - 0.5;
404
    int16_t dv = dx*p->s + dy*p->c - 0.5;
405 211 up20180642
    *u = du + basic_sprite_get_u0(p->bsp);
406
    *v = dv + basic_sprite_get_v0(p->bsp);
407
}
408
void (sprite_pic2src)(const sprite_t *p, int16_t u, int16_t v, int16_t *x, int16_t *y){
409
    int16_t du = u - basic_sprite_get_u0(p->bsp);
410
    int16_t dv = v - basic_sprite_get_v0(p->bsp);
411
    double dx =  du*p->c + dv*p->s;
412
    double dy = -du*p->s + dv*p->c;
413
    *x = dx*p->scale + 0.5 + p->x;
414
    *y = dy*p->scale + 0.5 + p->y;
415
}
416
void (sprite_draw)(const sprite_t *p){
417
    const uint16_t w = basic_sprite_get_w(p->bsp);
418
    const uint16_t h = basic_sprite_get_h(p->bsp);
419
    int16_t xmin, xmax, ymin, ymax; {
420
        int16_t x, y;
421
        sprite_pic2src(p, 0, 0, &x, &y);
422
        xmin = x; xmax = x; ymin = y; ymax = y;
423
        sprite_pic2src(p, w, 0, &x, &y);
424
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
425
        sprite_pic2src(p, 0, h, &x, &y);
426
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
427
        sprite_pic2src(p, w, h, &x, &y);
428
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
429 214 up20180642
        xmin = max(xmin-(int16_t)p->scale-2, 0); xmax = min(xmax+(int16_t)p->scale+2, graph_get_XRes());
430
        ymin = max(ymin-(int16_t)p->scale-2, 0); ymax = min(ymax+(int16_t)p->scale+2, graph_get_YRes());
431 211 up20180642
    }
432
    const uint8_t *map = basic_sprite_get_map(p->bsp);
433 250 up20180655
    const uint16_t bytes_pixel = 3/*graph_get_bytes_pixel()*/;
434 211 up20180642
    for(int16_t u, v, y = ymin; y < ymax; ++y){
435 212 up20180642
        uint8_t *place = video_buf + (xmin + y*graph_get_XRes())*bytes_pixel;
436
        for(int16_t x = xmin; x < xmax; ++x, place += bytes_pixel){
437 211 up20180642
            sprite_src2pic(p, x, y, &u, &v);
438
            if(0 <= u && u < w && 0 <= v && v < h){
439 212 up20180642
                const uint8_t *c_p = map+(v*w+u)*4;
440 216 up20180642
                if(*(c_p+3) < ALPHA_THRESHOLD) //alpha
441 212 up20180642
                    memcpy(place, c_p, bytes_pixel);
442 211 up20180642
            }
443
        }
444
    }
445
}