|
| 1 | +/* |
| 2 | + */ |
| 3 | + |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | + |
| 7 | +static char *bitmap; |
| 8 | +static int max_x, max_y; |
| 9 | +static float scale_x, scale_y; |
| 10 | + |
| 11 | +typedef unsigned int Uint32; |
| 12 | + |
| 13 | + |
| 14 | +#define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1))) |
| 15 | +#define ABS(x) ((x)>0 ? (x) : (-x)) |
| 16 | + |
| 17 | +int _scalex(int x) { |
| 18 | +#if 0 |
| 19 | + x = ((float)x)*scale_x; |
| 20 | +#endif |
| 21 | + x += max_x / 2; |
| 22 | + |
| 23 | + if (x < 0) x = 0; |
| 24 | + else |
| 25 | + if (x > max_x) x = max_x; |
| 26 | + return x; |
| 27 | +} |
| 28 | + |
| 29 | +int _scaley(int y) { |
| 30 | +#if 0 |
| 31 | + y = ((float)y)*scale_y; |
| 32 | +#endif |
| 33 | + y = -y; |
| 34 | + y += max_y / 2; |
| 35 | + |
| 36 | + if (y < 0) y = 0; |
| 37 | + else |
| 38 | + if (y > max_y) y = max_y; |
| 39 | + return y; |
| 40 | +} |
| 41 | + |
| 42 | +/* Basic unantialiased Bresenham line algorithm */ |
| 43 | +void bresenham_line(Uint32 x1, Uint32 y1, Uint32 x2, Uint32 y2, Uint32 color) |
| 44 | +{ |
| 45 | + int lg_delta, sh_delta, cycle, lg_step, sh_step; |
| 46 | + |
| 47 | + lg_delta = x2 - x1; |
| 48 | + sh_delta = y2 - y1; |
| 49 | + lg_step = SGN(lg_delta); |
| 50 | + lg_delta = ABS(lg_delta); |
| 51 | + sh_step = SGN(sh_delta); |
| 52 | + sh_delta = ABS(sh_delta); |
| 53 | + if (sh_delta < lg_delta) { |
| 54 | + cycle = lg_delta >> 1; |
| 55 | + while (x1 != x2) { |
| 56 | + sdl_dot(_scalex(x1), _scaley(y1), color); |
| 57 | + cycle += sh_delta; |
| 58 | + if (cycle > lg_delta) { |
| 59 | + cycle -= lg_delta; |
| 60 | + y1 += sh_step; |
| 61 | + } |
| 62 | + x1 += lg_step; |
| 63 | + } |
| 64 | + sdl_dot(_scalex(x1), _scaley(y1), color); |
| 65 | + } |
| 66 | + cycle = sh_delta >> 1; |
| 67 | + while (y1 != y2) { |
| 68 | + sdl_dot(_scalex(x1), _scaley(y1), color); |
| 69 | + cycle += lg_delta; |
| 70 | + if (cycle > sh_delta) { |
| 71 | + cycle -= sh_delta; |
| 72 | + x1 += lg_step; |
| 73 | + } |
| 74 | + y1 += sh_step; |
| 75 | + } |
| 76 | + sdl_dot(_scalex(x1), _scaley(y1), color); |
| 77 | +} |
| 78 | + |
| 79 | +void |
| 80 | +draw_line(int x1, int y1, int x2, int y2) |
| 81 | +{ |
| 82 | + //printf("line (%d,%d) -> (%d,%d)\n", x1, y1, x2, y2); |
| 83 | + bresenham_line(x1, y1, x2, y2, 1); |
| 84 | +} |
| 85 | + |
| 86 | +void |
| 87 | +draw_line_color(int x1, int y1, int x2, int y2) |
| 88 | +{ |
| 89 | + //printf("line (%d,%d) -> (%d,%d)\n", x1, y1, x2, y2); |
| 90 | + bresenham_line(x1, y1, x2, y2, 2); |
| 91 | +} |
| 92 | + |
| 93 | +void |
| 94 | +draw_marker(int x,int y, int size) |
| 95 | +{ |
| 96 | + draw_line(x-size, y, x+size, y); |
| 97 | + draw_line(x, y-size, x, y+size); |
| 98 | +} |
| 99 | + |
| 100 | +void |
| 101 | +draw_text(int x, int y, int font, int size, char *text) |
| 102 | +{ |
| 103 | + if (size < 8) size = 8; |
| 104 | + sdl_text(_scalex(x), _scaley(y), font, size, text); |
| 105 | +// draw_marker(x, y, 2); |
| 106 | +// sdl_dot(_scalex(x), _scaley(y), 1); |
| 107 | +} |
| 108 | + |
| 109 | +int |
| 110 | +draw_init(void) |
| 111 | +{ |
| 112 | + sdl_display_init(); |
| 113 | + sdl_get_bitmap(&bitmap); |
| 114 | + sdl_getsize(&max_x, &max_y); |
| 115 | + |
| 116 | + printf("max %d %d\n", max_x, max_y); |
| 117 | + |
| 118 | +#define SCALE 1.0 |
| 119 | + scale_x = SCALE; |
| 120 | + scale_y = SCALE; |
| 121 | + |
| 122 | +#if 0 |
| 123 | + draw_text(10,10,1,20,"at10,10"); |
| 124 | + draw_text(50,50,1,20,"at50,50"); |
| 125 | + draw_text(100,100,1,20,"at100,100"); |
| 126 | + while (1) |
| 127 | + draw_wait(); |
| 128 | +#endif |
| 129 | + |
| 130 | + return 0; |
| 131 | +} |
| 132 | + |
| 133 | +void |
| 134 | +draw_update(void) |
| 135 | +{ |
| 136 | + sdl_update_all(); |
| 137 | + sdl_poll(); |
| 138 | +} |
| 139 | + |
| 140 | +void |
| 141 | +draw_clear(void) |
| 142 | +{ |
| 143 | + sdl_setup_display(); |
| 144 | +} |
| 145 | + |
| 146 | +void |
| 147 | +draw_wait(void) |
| 148 | +{ |
| 149 | + printf("draw wait\n"); |
| 150 | + |
| 151 | + sdl_update_all(); |
| 152 | + while (1) { |
| 153 | + sdl_poll(); |
| 154 | + } |
| 155 | +} |
| 156 | + |
0 commit comments