Project

General

Profile

Statistics
| Revision:

root / proj / libs / graph / src / graph.c @ 335

History | View | Annotate | Download (14.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 324 up20180642
#define MBYTE_BASE  0x0         /** @brief Base address (zero address) */
12
#define MBYTE_SIZE  0xFFFFF     /** @brief Size of a mebibyte */
13 253 up20180642
14 333 up20180642
#define LINEAR_FRAME_BUFFER_MD  BIT(14)
15
16 253 up20180642
// Graphics Functions
17 324 up20180642
#define VBE_CTRL_INFO       0x00    /** @brief Get VBE Controller Information */
18 253 up20180642
#define VBE_MD_INFO         0x01    /** @brief Get VBE Mode Information */
19
#define SET_VBE_MD          0x02    /** @brief Set VBE Mode */
20
21
// Error codes (AH)
22
#define AH_SUCCESS          0x00    /** @brief Success code on BIOS call */
23 324 up20180642
#define AH_FUNC_CALL_FAIL   0x01    /** @brief Function call failed */
24
#define AH_FUNC_NOT_SUPP    0x02    /** @brief Function call is not supported in current HW configuration */
25
#define AH_FUNC_INVALID     0x03    /** @brief Invalid function in current video mode */
26 253 up20180642
27 174 up20180642
/// MACROS
28 324 up20180642
#define FAR2PHYS(n)         ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF))
29 174 up20180642
30
/// STRUCT
31
typedef struct __attribute__((packed)) {
32
33
    char        VbeSignature[4]     ;
34
    uint16_t    VbeVersion          ;
35
    uint32_t    OemStringPtr        ;
36
    uint8_t     Capabilities[4]     ;
37
    uint32_t    VideoModePtr        ;
38
    uint16_t    TotalMemory         ;
39
40
    uint16_t    OemSoftwareRev      ;
41
    uint32_t    OemVendorNamePtr    ;
42
    uint32_t    OemProductNamePtr   ;
43
    uint32_t    OemProductRevPtr    ;
44
    char        Reserved[222]       ;
45
46
    char        OemData[256]        ;
47
} VbeInfoBlock;
48
49 168 up20180642
static vbe_mode_info_t vbe_mem_info;
50
51 174 up20180642
/// PRIVATE GET
52
static uint16_t   (graph_get_bits_pixel)   (void){ return vbe_mem_info.BitsPerPixel; }
53
static phys_bytes (graph_get_phys_addr)    (void){ return vbe_mem_info.PhysBasePtr; }
54
static unsigned   (graph_get_vram_size)    (void){ return vbe_mem_info.XResolution * vbe_mem_info.YResolution * graph_get_bytes_pixel(); }
55
//static uint16_t   (graph_get_RedMaskSize)  (void){ return vbe_mem_info.RedMaskSize  ; }
56
//static uint16_t   (graph_get_GreenMaskSize)(void){ return vbe_mem_info.GreenMaskSize; }
57
//static uint16_t   (graph_get_BlueMaskSize) (void){ return vbe_mem_info.BlueMaskSize ; }
58
59 208 up20180642
/// PUBLIC GET
60
uint16_t   (graph_get_XRes)         (void){ return vbe_mem_info.XResolution; }
61
uint16_t   (graph_get_YRes)         (void){ return vbe_mem_info.YResolution; }
62
uint16_t   (graph_get_bytes_pixel)  (void){ return (graph_get_bits_pixel() + 7) >> 3; }
63
64
///
65 174 up20180642
static int (get_permission)(unsigned int base_addr, unsigned int size) {
66 168 up20180642
    struct minix_mem_range mmr;
67
    mmr.mr_base = base_addr;
68
    mmr.mr_limit = base_addr + size;
69
    return sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr);
70
}
71
72 174 up20180642
//static int (get_permissions_first_mbyte)(void) {
73 168 up20180642
//    return get_permission(MBYTE_BASE, MBYTE_SIZE);
74
//}
75
76 174 up20180642
/// MEMORY
77 178 up20180642
static uint8_t *video_mem = NULL; /** @brief Frame-buffer VM address. */
78 174 up20180642
static uint8_t *video_buf = NULL; /** @brief Primary buffer for drawing before copying to video_mem. */
79
static mmap_t mem_map;
80
static int (graph_free_memory)(void) {
81
    int r = SUCCESS;
82
    free(video_buf); video_buf = NULL;
83
    r = !lm_free(&mem_map);
84
    return r;
85
}
86
static int (graph_map_vram)(void) {
87
    int r;
88
    const unsigned vram_base = graph_get_phys_addr();
89
    const unsigned vram_size = graph_get_vram_size();
90
    if ((r = get_permission(vram_base, vram_size))) {
91
        if (graph_free_memory()) {
92
            printf("%s: lm_free failed\n", __func__);
93
        }
94
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
95 168 up20180642
    }
96
97 174 up20180642
    video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
98 168 up20180642
99 174 up20180642
    if (video_mem == MAP_FAILED) {
100
        if (graph_free_memory()) {
101
            printf("%s: lm_free failed\n", __func__);
102
        }
103
        panic("%s: couldn't map video memory.", __func__);
104
    }
105
106
    video_buf = malloc(vram_size);
107
108 168 up20180642
    return SUCCESS;
109
}
110
111 174 up20180642
/// INFO GET
112
static int (vbe_get_mode_information)(uint16_t mode) {
113 168 up20180642
    memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // reset values
114
115
    struct reg86 reg_86;
116
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
117
118
    vbe_mode_info_t *virtual_addr = lm_alloc(sizeof(vbe_mode_info_t), &mem_map);
119
120
    reg_86.intno = VC_BIOS_SERV;
121
    reg_86.ah = VBE_CALL;
122
    reg_86.al = VBE_MD_INFO;
123
    reg_86.cx = mode;
124
    reg_86.es = PB2BASE(mem_map.phys);
125
    reg_86.di = PB2OFF(mem_map.phys);
126
    // BIOS CALL
127
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
128
        printf("%s: sys_int86 failed\n", __func__);
129
        if (graph_free_memory()) {
130
            printf("%s: lm_free failed\n", __func__);
131
        }
132
        return BIOS_CALL_ERROR;
133
    }
134
135
    memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size);
136
    return SUCCESS;
137
}
138 174 up20180642
/*
139
static int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) {
140 168 up20180642
    memset(info_p, 0, sizeof(vg_vbe_contr_info_t)); // reset values
141

142
    mmap_t controller_map;
143

144
    struct reg86 reg_86;
145
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
146

147
    VbeInfoBlock *virtual_addr = lm_alloc(sizeof(VbeInfoBlock), &controller_map);
148

149
    uint32_t virtual_base = (uint32_t)(virtual_addr) - controller_map.phys;
150

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

156

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

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

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

182
    info_p->TotalMemory = (virtual_addr->TotalMemory << 6);
183

184
    // Convert Far Far Pointer to Virtual Address
185

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

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

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

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

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

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

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