From 7106605e140f508926cc849ead43841e1f11339e Mon Sep 17 00:00:00 2001 From: Gladosse Date: Sat, 29 Jan 2022 18:27:41 +0100 Subject: [PATCH] Update 'gint/main.c' --- gint/main.c | 161 +++++++++++++++++++++++++--------------------------- 1 file changed, 76 insertions(+), 85 deletions(-) diff --git a/gint/main.c b/gint/main.c index 656b9aa..7fd51b7 100644 --- a/gint/main.c +++ b/gint/main.c @@ -6,17 +6,21 @@ #define SCREEN_WIDTH 384 #define GET_POS(x) ((abs(x)+x)/2) #define SEG_WIDTH 70 -#define C_RGB(r,g,b) (((r) << 11) | ((g) << 6) | (b)) +#define C_RGB(r,g,b) ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3) +#define SCREEN_HEIGHT 216 +#define SCREEN_WIDTH 384 +#define GET_POS(x) ((abs(x)+x)/2) +#define SEG_WIDTH 70 +#define CH_Z 9 const float CH_H = 4; -const float CH_W = 1.5; -const float dt = 0.05; // ;-; -float CH_HY = 0.70; //0->flat - -int gray = C_RGB(155, 154, 166); -int lightgray = C_RGB(183, 187, 201); -int red = C_RGB(255, 46, 88); -int lightblue = C_RGB(173, 216, 230); +const float CH_W = 1.3; +float CH_HY = 0.80; +const float dt = 0.01; +int gray = C_RGB(131, 118, 156); +int lightgray = C_RGB(194, 195, 199); +int red = C_RGB(255, 0, 77); +int lightblue = C_RGB(41, 173, 255); struct Road { int height; //number of elements @@ -26,6 +30,7 @@ struct Road { int width; int segment_height; int starty; + int startx; }; struct Segment { @@ -37,20 +42,73 @@ struct Segment { int colour; }; -struct Segment seg(float, float, float, int, int, int); -void draw_road(struct Road*); -void gen_road(struct Road*); +struct Segment seg(float x, float y, float z, int width, int height, int colour) { + struct Segment segment = { x, y, z, width, height, colour }; + return segment; +} + +void gen_road(unsigned char* grid, int size, struct Road *road) { + int v = 0; + for (int i = 0;i < size;i++) { + uint8_t cell = grid[i]; + unsigned char low = cell & 0x0F; + unsigned char high = cell >> 4; + if (high == 0) { + road->segments[v] = seg(0, 190 - low * 5, i * CH_Z, SEG_WIDTH, 20, gray); + } + else if (high == 1) { + road->segments[v++] = seg(0, 190 - low * 5, i * CH_Z, SEG_WIDTH, 20, gray); + float z = i*CH_Z+CH_Z; + float s = SEG_WIDTH - z / (CH_H * CH_W); + float w31 = 120 * (s / SEG_WIDTH); + float w32 = (120 - SEG_WIDTH) / 2; + float h31 = 70 * (s / SEG_WIDTH); + float w30 = SEG_WIDTH * (s / SEG_WIDTH); + float h30 = 20 * (s / SEG_WIDTH); + float y31 = road->segments[v-1].y + h30; + float h32 = w32 * (s / SEG_WIDTH); + road->segments[v++] = seg((w31 - w30) / -2, y31, z, 120, 50, lightgray); + road->segments[v++] = seg((w31 - w30) / -2, y31 - h31, z, w32, 70, lightgray); + road->segments[v++] = seg( w30, y31 - h31, z, w32, 70, lightgray); + road->segments[v++] = seg(w30, y31 - h31 - h32, z, w32, w32, red); + road->segments[v] = seg((w31 - w30) / -2, y31 - h31 - h32, z, w32, w32, red); + } + v++; + } + +} + +void draw_road(struct Road* road) { + float s; + for (int v = road->height - 1;v >= 0;v--) { //draws each rectangle from furthest to closest + s = SEG_WIDTH - road->segments[v].z / (CH_H * CH_W); + float yoffset = road->segments[v].y - road->segments[v].z * CH_HY + road->playery; + float heightoffset = road->segments[v].height * (s / SEG_WIDTH); + float widthoffset = road->segments[v].width * (s / SEG_WIDTH); + float xoffset = ((road->segments[v].x + SCREEN_WIDTH / 2) - s / 2) - road->playerx * (s - road->segments[v].z / 10); + + if (yoffset > SCREEN_HEIGHT || yoffset + heightoffset < 0 || xoffset + widthoffset < 0 || xoffset - widthoffset > SCREEN_WIDTH) + continue; + + drect(xoffset, yoffset, xoffset + widthoffset, yoffset+heightoffset, road->segments[v].colour); + if (road->segments[v].colour == gray) + drect(xoffset + s / 2, yoffset + 2, xoffset + s / 2 + 3, yoffset+2+3, C_WHITE); + } +} int main() { struct Road road; + road.height = 32; road.playerx = 0; road.playery = 0; road.width = 100; road.starty = 160; + road.startx = 40; road.segment_height = 30; - road.height = 27; road.segments = (struct Segment*)malloc(road.height * sizeof(struct Segment)); - gen_road(&road); + + unsigned char grid[] = {0x00,0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x14,0x04,0x04,0x03,0x02, 0x01,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00}; + gen_road(grid, sizeof(grid), &road); while (!keydown(KEY_MENU)) { dclear(lightblue); @@ -63,9 +121,9 @@ int main() { const int down_down = keydown(KEY_DOWN); //pardon lephenixnoir :E if (right_down) - road.playerx -= 5 * dt; + road.playerx -= 10 * dt; else if (left_down) - road.playerx += 5 * dt; + road.playerx += 10 * dt; else if (up_down) { road.playery -= 250 * dt; @@ -80,72 +138,5 @@ int main() { return 1; } -struct Segment seg(float x, float y, float z, int width, int height, int colour) { - struct Segment segment = { x, y, z, width, height, colour }; - return segment; -} - -void gen_road(struct Road* road) { - road->segments[0] = seg(0, 190, 0, SEG_WIDTH, 20, gray); - road->segments[1] = seg(0, 190, 9, SEG_WIDTH, 20, gray); - road->segments[2] = seg(0, 190, 18, SEG_WIDTH, 20, gray); - road->segments[3] = seg(0, 190, 27, SEG_WIDTH, 20, gray); - road->segments[4] = seg(0, 190, 36, SEG_WIDTH, 20, gray); - road->segments[5] = seg(0, 190, 45, SEG_WIDTH, 20, gray); - road->segments[6] = seg(0, 185, 54, SEG_WIDTH, 20, gray); - road->segments[7] = seg(0, 180, 63, SEG_WIDTH, 20, gray); - road->segments[8] = seg(0, 178, 72, SEG_WIDTH, 20, gray); - /*pillar*/ - float s = SEG_WIDTH - 81 / CH_H; - float w31 = 136 * (s / SEG_WIDTH); - float h31 = 50 * (s / SEG_WIDTH); - float w30 = SEG_WIDTH * (s / SEG_WIDTH); - float h30 = 20 * (s / SEG_WIDTH); - float y31 = 178 + h30; - float h32 = 30 * (s / SEG_WIDTH); - road->segments[9] = seg((w31 - w30) / -2, y31, 81, 136, 50, lightgray); - road->segments[10] = seg((w31 - w30) / -2, y31 - h31, 81, 33, 70, lightgray); - road->segments[11] = seg(w30, y31 - h31, 81, 33, 70, lightgray); - road->segments[12] = seg(w30, y31 - h31 - h32, 81, 33, 30, red); - road->segments[13] = seg((w31 - w30) / -2, y31 - h31 - h32, 81, 33, 30, red); - /*pillar*/ - road->segments[14] = seg(0, 175, 81, SEG_WIDTH, 20, gray); - road->segments[15] = seg(0, 175, 90, SEG_WIDTH, 20, gray); - road->segments[16] = seg(0, 180, 99, SEG_WIDTH, 20, gray); - road->segments[17] = seg(0, 190, 108, SEG_WIDTH, 20, gray); - road->segments[18] = seg(0, 190, 117, SEG_WIDTH, 20, gray); - /*pillar*/ - s = SEG_WIDTH - 117 / (CH_H); - w31 = 136 * (s / SEG_WIDTH); - h31 = 50 * (s / SEG_WIDTH); - w30 = SEG_WIDTH * (s / SEG_WIDTH); - h30 = 20 * (s / SEG_WIDTH); - y31 = 190 + h30; - h32 = 30 * (s / SEG_WIDTH); - road->segments[19] = seg((w31 - w30) / -2, y31, 117, 136, 50, lightgray); - road->segments[20] = seg((w31 - w30) / -2, y31 - h31, 117, 33, 70, lightgray); - road->segments[21] = seg(w30, y31 - h31, 117, 33, 70, lightgray); - road->segments[22] = seg(w30, (y31 - h31) - h32, 117, 33, 30, red); - road->segments[23] = seg((w31 - w30) / -2, (y31 - h31) - h32, 117, 33, 30, red); - /*pillar*/ - road->segments[24] = seg(0, 190, 126, SEG_WIDTH, 20, gray); - road->segments[25] = seg(0, 190, 135, SEG_WIDTH, 20, gray); - road->segments[26] = seg(0, 190, 144, SEG_WIDTH, 20, gray); - -} - -void draw_road(struct Road* road) { - float s; - for (int v = road->height - 1;v >= 0;v--) { //draws each rectangle from furthest to closest - float yoffset = road->segments[v].y - road->segments[v].z * CH_HY + road->playery; - s = SEG_WIDTH - road->segments[v].z / (CH_H); - float heightoffset = road->segments[v].height * (s / SEG_WIDTH); - if(yoffset < SCREEN_HEIGHT && yoffset+heightoffset < 0) - continue; - float widthoffset = road->segments[v].width * (s / SEG_WIDTH); - float xoffset = ((road->segments[v].x + SCREEN_WIDTH / 2) - s / 2) - road->playerx * s; - drect(xoffset, yoffset, xoffset + widthoffset, yoffset+heightoffset, road->segments[v].colour); - if (road->segments[v].colour == gray) - drect(xoffset + s / 2, yoffset + 2, xoffset + s / 2 + 3, yoffset+2+3, C_WHITE); - } -} \ No newline at end of file +//hey MR +//yo duck goose \ No newline at end of file