From bc735da7902a75f6428425039bec542fda062f7b Mon Sep 17 00:00:00 2001 From: Gladosse Date: Sun, 13 Feb 2022 13:57:14 +0100 Subject: [PATCH] Update 'sfml/main.cpp' --- sfml/main.cpp | 158 ++++++++++++++++++++------------------------------ 1 file changed, 63 insertions(+), 95 deletions(-) diff --git a/sfml/main.cpp b/sfml/main.cpp index 69c7df5..a2f79f1 100644 --- a/sfml/main.cpp +++ b/sfml/main.cpp @@ -1,103 +1,38 @@ #include #include +#include -#define SCREEN_HEIGHT 216 -#define SCREEN_WIDTH 384 +#define SCREEN_HEIGHT 224 +#define SCREEN_WIDTH 396 #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; -float CH_HY = 0.70; //0->flat +const float CH_W = 1.65; +float CH_HY = 0.80; //0->flat -sf::Color gray = sf::Color(84, 90, 112); -sf::Color lightgray = sf::Color(183, 187, 201); -sf::Color red = sf::Color(255, 46, 88); +sf::Color gray = sf::Color(131, 118, 156); +sf::Color lightgray = sf::Color(194, 195, 199); +sf::Color red = sf::Color(255, 0, 77); +sf::Color blue = sf::Color(41, 173, 255); -struct Road { - int height; //number of elements - struct Segment* segments; //array of segments - float playerx; - float playery; - int width; - int segment_height; - int starty; -}; - -struct Segment { - float x; - float y; - float z; - int width; - int height; - sf::Color colour; -}; - -struct Segment seg(float x, float y, float z, int width, float height, sf::Color colour) { +struct Segment seg(float x, float y, float z, int width, int height, sf::Color colour) { struct Segment segment = { x, y, z, width, height, colour }; return segment; } -void gen_road(struct Road* road, float zm = 1) { - 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(sf::RenderWindow* window, struct Road* road, float zm = 2) { +void draw_road(sf::RenderWindow* window, 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; - 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 - road->segments[v].z/10); + 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; + sf::RectangleShape rs(sf::Vector2f(widthoffset, heightoffset)); //makes a rectangle of width s and height relative to z position rs.setFillColor(road->segments[v].colour); rs.setPosition(xoffset, yoffset); @@ -111,17 +46,52 @@ void draw_road(sf::RenderWindow* window, struct Road* road, float zm = 2) { } } + +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++; + } +} + int main() { sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "KraD"); struct Road road; + road.height = 64; 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, CH_H); //2nd param = change rate of height + + 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,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); + sf::Clock clock; while (window.isOpen()) { @@ -134,29 +104,27 @@ int main() { } if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { - road.playerx -= 5 * dt; + road.playerx -= 4 * dt; } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { - road.playerx += 5 * dt; + road.playerx += 4 * dt; } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { - float mov = 300 * dt; for (int i = 0;i < road.height;i++) { - road.segments[i].x -= mov; + road.segments[i].x -= 300 * dt; } } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { - float mov = 300 * dt; for (int i = 0;i < road.height;i++) { - road.segments[i].x += mov; + road.segments[i].x += 300 * dt; } } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { - road.playery -= 250 * dt; + road.playery -= 250 * dt; CH_HY -= 5 / 4 * dt; } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) @@ -172,8 +140,8 @@ int main() { { road.playery += 250 * dt; } - window.clear(sf::Color(0, 191, 255)); - draw_road(&window, &road, CH_W); //3rd param = change rate of width + window.clear(blue); + draw_road(&window, &road); //3rd param = change rate of width window.display(); } -} +} \ No newline at end of file