diff --git a/include/ui/draw_images.h b/include/ui/draw_images.h new file mode 100644 index 0000000000..69cef56a35 --- /dev/null +++ b/include/ui/draw_images.h @@ -0,0 +1,34 @@ +#ifndef DRAW_IMAGES_H +#define DRAW_IMAGES_H + +#include "game-state/room.h" + +/* draw_room_gui + * Draws a room based on its room id for the split screen in chiventure + * + * Parameters: + * - width: integer that defines the width of the split screen + * - height: integer that defines the height of the split screen + * - pos_x: integer that defines the x-coordinate of the center of the image + * - pos_y: integer that defines the x-coordinate of the center of the image + * - filepath: string that contains the filepath to the directory containing the images + * - curr_room: room_t that defines the current room the player is in + * + * No value is returned + */ +void draw_room_gui(int width, int height, int pos_x, int pos_y, char *filepath, room_t *curr_room); + +/* draw_map + * Draws the map in the top left corner of the split screen that updates the center room + * when the player moves rooms + * + * Parameters: + * - width: integer that defines the width of the split screen + * - height: integer that defines the height of the split screen + * - curr_room: room_t that defines the current room the player is in + * + * No value is returned + */ +void draw_map(int width, int height, room_t *curr_room); + +#endif \ No newline at end of file diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index 24531dddbf..eb95ef75d4 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -5,7 +5,8 @@ add_library(ui src/print_functions.c src/ui.c src/ui_ctx.c - src/window.c) + src/window.c + src/draw_images.c) if (raylib_FOUND) target_sources(ui PRIVATE src/gui.c) diff --git a/src/ui/examples/CMakeLists.txt b/src/ui/examples/CMakeLists.txt index 0104e7cfca..a8c115abeb 100644 --- a/src/ui/examples/CMakeLists.txt +++ b/src/ui/examples/CMakeLists.txt @@ -6,7 +6,7 @@ if (raylib_FOUND) add_executable(split_screen_example split_screen_example.c) add_executable(split_screen_map_example - split_screen_map_example.c) + split_screen_map_example.c) add_executable(gui_sample_game gui_sample_game.c) diff --git a/src/ui/examples/gui_sample_game.c b/src/ui/examples/gui_sample_game.c index ff56b1af8d..491110e17b 100644 --- a/src/ui/examples/gui_sample_game.c +++ b/src/ui/examples/gui_sample_game.c @@ -6,6 +6,7 @@ #include "raylib.h" #include "game-state/game.h" #include "game-state/room.h" +#include "ui/draw_images.h" chiventure_ctx_t *create_sample_ctx() @@ -134,27 +135,10 @@ int main() { int height = SCREEN_HEIGHT/2; int pos_x = SCREEN_WIDTH/4; int pos_y = SCREEN_HEIGHT/10; + room_t *curr_room = ctx->game->curr_room; + char *filepath = strdup("../../../../src/ui/examples/"); - /* Loading just one image is a temporary solution - In the future, we will use a more generic path so that we can load - game-specific images */ - char filename[100] = "../../../../src/ui/examples/"; - - strcat(filename, ctx->game->curr_room->room_id); - - strcat(filename, ".png"); - - Image room = LoadImage(filename); - - ImageResize(&room, width, height); - - /* Image converted to texture, uploaded to GPU memory (VRAM) */ - Texture2D texture = LoadTextureFromImage(room); - - /* Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM */ - UnloadImage(room); - - DrawTexture(texture, pos_x, pos_y, WHITE); + draw_room_gui(width, height, pos_x, pos_y, filepath, curr_room); DrawRectangleRec(textBox, WHITE); DrawRectangle(POS_ZERO, SCREEN_HEIGHT - heightbuf2, SCREEN_WIDTH, rectHeight, WHITE); @@ -180,117 +164,10 @@ int main() { Font test = GetFontDefault(); DrawTextRec(test, output_text, output, fontSize, fontSpacing, true, BLACK); - int map_width, map_height, map_room_width, map_room_height, map_topX, map_topY, ball_rad; - map_width = height / 2; - map_height = width / 5; - map_room_width = map_width / 3; - map_room_height = map_height / 3; - map_topX = 5; - map_topY = 5; - ball_rad = map_room_width / 10; - room_t *curr_room = ctx->game->curr_room; - - Color colors[8]; - - colors[0] = RED; - colors[1] = PINK; - colors[2] = PURPLE; - colors[3] = BLUE; - colors[4] = YELLOW; - colors[5] = GREEN; - colors[6] = ORANGE; - colors[7] = DARKGREEN; - - // map background - DrawRectangle(map_topX, map_topY, map_width, map_height, BLACK); - - int posX = map_topX + map_width/2 - map_room_width/2; - int posY = map_topY + map_height/2 - map_room_height/2; - // current room - DrawRectangle(posX, posY, map_room_width, map_room_height, colors[0]); - - //draw surrounding rooms around current room - if (find_room_from_dir(curr_room, "EAST") != NULL) - { - int tempX = posX + map_room_width; - DrawRectangle(tempX, posY, map_room_width, map_room_height, colors[1]); - - room_t *east = find_room_from_dir(curr_room, "EAST"); - // draw rooms above and below the room that is east of the current room - // northeast and southeast rooms to the current room - if (find_room_from_dir(east, "NORTH") != NULL) - { - int tempY = posY - map_room_height; - DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[5]); - } - if (find_room_from_dir(east, "SOUTH") != NULL) - { - int tempY = posY + map_room_height; - DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[6]); - } - - } - if (find_room_from_dir(curr_room, "WEST") != NULL) - { - int tempX = posX - map_room_width; - DrawRectangle(tempX, posY, map_room_width, map_room_height, colors[2]); - - room_t *west = find_room_from_dir(curr_room, "WEST"); - // draw rooms above and below the room that is west of the current room - // northwest and southwest rooms to the current room - if (find_room_from_dir(west, "NORTH") != NULL) - { - int tempY = posY - map_room_height; - DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[7]); - } - if (find_room_from_dir(west, "SOUTH") != NULL) - { - int tempY = posY + map_room_height; - DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[8]); - } - } - if (find_room_from_dir(curr_room, "SOUTH") != NULL) - { - int tempY = posY + map_room_height; - DrawRectangle(posX, tempY, map_room_width, map_room_height, colors[3]); - - room_t *south = find_room_from_dir(curr_room, "SOUTH"); - // draw rooms next to the room that is south of the current room - // southeast and southwest rooms to the current room - if (find_room_from_dir(south, "EAST") != NULL) - { - int tempX = posX + map_room_width; - DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[6]); - } - if (find_room_from_dir(south, "WEST") != NULL) - { - int tempX = posX - map_room_width; - DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[8]); - } - } - if (find_room_from_dir(curr_room, "NORTH") != NULL) - { - int tempY = posY - map_room_height; - DrawRectangle(posX, tempY, map_room_width, map_room_height, colors[4]); - - room_t *north = find_room_from_dir(curr_room, "NORTH"); - // draw rooms next to the room that is north of the current room - // northeast and northwest rooms to the current room - if (find_room_from_dir(north, "WEST") != NULL) - { - int tempX = posX + map_room_width; - DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[7]); - } - if (find_room_from_dir(north, "EAST") != NULL) - { - int tempX = posX + map_room_width; - DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[5]); - } - } + /* Drawing the Map for the upper half of the split screen */ + + draw_map(width, height, curr_room); - //draws player position as ball in the middle of map screen and inside current room - DrawCircle(map_topX + map_width/2, map_topY + map_height/2, ball_rad, WHITE); - EndDrawing(); } diff --git a/src/ui/src/draw_images.c b/src/ui/src/draw_images.c new file mode 100644 index 0000000000..7bdb14af0d --- /dev/null +++ b/src/ui/src/draw_images.c @@ -0,0 +1,144 @@ +#include +#include +#include "raylib.h" +#include "ui/draw_images.h" + + +#define MAX_FILENAME_LEN (100) + +/* See draw_images.h for documentation */ +void draw_room_gui(int width, int height, int pos_x, int pos_y, char *filepath, room_t *curr_room) +{ + + + char *image_filename = calloc(100, sizeof(char)); + sprintf(image_filename, "%s%s.%s", filepath, curr_room->room_id, "png"); + + Image room = LoadImage(filepath); + + ImageResize(&room, width, height); + + /* Image converted to texture, uploaded to GPU memory (VRAM) */ + Texture2D texture = LoadTextureFromImage(room); + + /* Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM */ + UnloadImage(room); + + DrawTexture(texture, pos_x, pos_y, WHITE); +} + +/* See draw_images.h for documentation */ +void draw_map(int width, int height, room_t *curr_room) +{ + int map_width, map_height, map_room_width, map_room_height, map_topX, map_topY, ball_rad; + map_width = height / 2; + map_height = width / 5; + map_room_width = map_width / 3; + map_room_height = map_height / 3; + map_topX = 5; + map_topY = 5; + ball_rad = map_room_width / 10; + + Color colors[8]; + + colors[0] = RED; + colors[1] = PINK; + colors[2] = PURPLE; + colors[3] = BLUE; + colors[4] = YELLOW; + colors[5] = GREEN; + colors[6] = ORANGE; + colors[7] = DARKGREEN; + + /* map background */ + DrawRectangle(map_topX, map_topY, map_width, map_height, BLACK); + + int posX = map_topX + map_width / 2 - map_room_width / 2; + int posY = map_topY + map_height / 2 - map_room_height / 2; + + /* draw current room */ + DrawRectangle(posX, posY, map_room_width, map_room_height, colors[0]); + + /* draw surrounding rooms around current room */ + if (find_room_from_dir(curr_room, "EAST") != NULL) + { + int tempX = posX + map_room_width; + DrawRectangle(tempX, posY, map_room_width, map_room_height, colors[1]); + + room_t *east = find_room_from_dir(curr_room, "EAST"); + /* draw rooms above and below the room that is east of the current room + northeast and southeast rooms to the current room */ + if (find_room_from_dir(east, "NORTH") != NULL) + { + int tempY = posY - map_room_height; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[5]); + } + if (find_room_from_dir(east, "SOUTH") != NULL) + { + int tempY = posY + map_room_height; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[6]); + } + } + if (find_room_from_dir(curr_room, "WEST") != NULL) + { + int tempX = posX - map_room_width; + DrawRectangle(tempX, posY, map_room_width, map_room_height, colors[2]); + + room_t *west = find_room_from_dir(curr_room, "WEST"); + /* draw rooms above and below the room that is west of the current room + northwest and southwest rooms to the current room */ + if (find_room_from_dir(west, "NORTH") != NULL) + { + int tempY = posY - map_room_height; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[7]); + } + if (find_room_from_dir(west, "SOUTH") != NULL) + { + int tempY = posY + map_room_height; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[8]); + } + } + if (find_room_from_dir(curr_room, "SOUTH") != NULL) + { + int tempY = posY + map_room_height; + DrawRectangle(posX, tempY, map_room_width, map_room_height, colors[3]); + + room_t *south = find_room_from_dir(curr_room, "SOUTH"); + /* draw rooms next to the room that is south of the current room + southeast and southwest rooms to the current room */ + if (find_room_from_dir(south, "EAST") != NULL) + { + int tempX = posX + map_room_width; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[6]); + } + if (find_room_from_dir(south, "WEST") != NULL) + { + int tempX = posX - map_room_width; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[8]); + } + } + if (find_room_from_dir(curr_room, "NORTH") != NULL) + { + int tempY = posY - map_room_height; + DrawRectangle(posX, tempY, map_room_width, map_room_height, colors[4]); + + room_t *north = find_room_from_dir(curr_room, "NORTH"); + /* draw rooms next to the room that is north of the current room + northeast and northwest rooms to the current room */ + if (find_room_from_dir(north, "WEST") != NULL) + { + int tempX = posX + map_room_width; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[7]); + } + if (find_room_from_dir(north, "EAST") != NULL) + { + int tempX = posX + map_room_width; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[5]); + } + } + + /* draws player position as ball in the middle of map screen and inside current room */ + DrawCircle(map_topX + map_width / 2, map_topY + map_height / 2, ball_rad, WHITE); +} + + diff --git a/src/ui/src/gui.c b/src/ui/src/gui.c index 41ca1fdb49..09501ffcf0 100644 --- a/src/ui/src/gui.c +++ b/src/ui/src/gui.c @@ -4,6 +4,7 @@ #include "action_management/actionmanagement.h" #include "ui/gui.h" #include "raylib.h" +#include "ui/draw_images.h" void run_gui(chiventure_ctx_t *ctx) @@ -49,7 +50,7 @@ void run_gui(chiventure_ctx_t *ctx) while (key > 0) { /* NOTE: Only allow keys in range [32..125} as these are the printable characters */ - if ((key >= 0) && (key <= 127) && (letterCount < MAX_INPUT_CHARS)) + if ((key >= 32) && (key <= 125) && (letterCount < MAX_INPUT_CHARS)) { name[letterCount] = (char)key; letterCount++; @@ -112,8 +113,8 @@ void run_gui(chiventure_ctx_t *ctx) /* Loading just one image is a temporary solution In the future, we will use a more generic path so that we can load game-specific images */ - Image room = LoadImage("../src/ui/src/chiventure.png"); - + Image room = LoadImage("../src/ui/src/chiventure.png"); + ImageResize(&room, width, height); /* Image converted to texture, uploaded to GPU memory (VRAM) */ @@ -148,9 +149,122 @@ void run_gui(chiventure_ctx_t *ctx) Font test = GetFontDefault(); DrawTextRec(test, output_text, output, fontSize, fontSpacing, true, BLACK); + + /* Start drawing map */ + int map_width, map_height, map_room_width, map_room_height, map_topX, map_topY, ball_rad; + map_width = height / 2; + map_height = width / 5; + map_room_width = map_width / 3; + map_room_height = map_height / 3; + map_topX = 5; + map_topY = 5; + ball_rad = map_room_width / 10; + room_t *curr_room = ctx->game->curr_room; + + Color colors[8]; + + colors[0] = RED; + colors[1] = PINK; + colors[2] = PURPLE; + colors[3] = BLUE; + colors[4] = YELLOW; + colors[5] = GREEN; + colors[6] = ORANGE; + colors[7] = DARKGREEN; + + /* map background */ + DrawRectangle(map_topX, map_topY, map_width, map_height, BLACK); + + int posX = map_topX + map_width/2 - map_room_width/2; + int posY = map_topY + map_height/2 - map_room_height/2; + /* draw current room */ + DrawRectangle(posX, posY, map_room_width, map_room_height, colors[0]); + + /* draw surrounding rooms around current room */ + if (find_room_from_dir(curr_room, "EAST") != NULL) + { + int tempX = posX + map_room_width; + DrawRectangle(tempX, posY, map_room_width, map_room_height, colors[1]); + + room_t *east = find_room_from_dir(curr_room, "EAST"); + /* draw rooms above and below the room that is east of the current room + northeast and southeast rooms to the current room */ + if (find_room_from_dir(east, "NORTH") != NULL) + { + int tempY = posY - map_room_height; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[5]); + } + if (find_room_from_dir(east, "SOUTH") != NULL) + { + int tempY = posY + map_room_height; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[6]); + } + + } + if (find_room_from_dir(curr_room, "WEST") != NULL) + { + int tempX = posX - map_room_width; + DrawRectangle(tempX, posY, map_room_width, map_room_height, colors[2]); + + room_t *west = find_room_from_dir(curr_room, "WEST"); + /* draw rooms above and below the room that is west of the current room + northwest and southwest rooms to the current room */ + if (find_room_from_dir(west, "NORTH") != NULL) + { + int tempY = posY - map_room_height; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[7]); + } + if (find_room_from_dir(west, "SOUTH") != NULL) + { + int tempY = posY + map_room_height; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[8]); + } + } + if (find_room_from_dir(curr_room, "SOUTH") != NULL) + { + int tempY = posY + map_room_height; + DrawRectangle(posX, tempY, map_room_width, map_room_height, colors[3]); + + room_t *south = find_room_from_dir(curr_room, "SOUTH"); + /* draw rooms next to the room that is south of the current room + southeast and southwest rooms to the current room */ + if (find_room_from_dir(south, "EAST") != NULL) + { + int tempX = posX + map_room_width; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[6]); + } + if (find_room_from_dir(south, "WEST") != NULL) + { + int tempX = posX - map_room_width; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[8]); + } + } + if (find_room_from_dir(curr_room, "NORTH") != NULL) + { + int tempY = posY - map_room_height; + DrawRectangle(posX, tempY, map_room_width, map_room_height, colors[4]); + + room_t *north = find_room_from_dir(curr_room, "NORTH"); + /* draw rooms next to the room that is north of the current room + northeast and northwest rooms to the current room */ + if (find_room_from_dir(north, "WEST") != NULL) + { + int tempX = posX + map_room_width; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[7]); + } + if (find_room_from_dir(north, "EAST") != NULL) + { + int tempX = posX + map_room_width; + DrawRectangle(tempX, tempY, map_room_width, map_room_height, colors[5]); + } + } + + /* draws player position as ball in the middle of map screen and inside current room */ + DrawCircle(map_topX + map_width/2, map_topY + map_height/2, ball_rad, WHITE); + EndDrawing(); } CloseWindow(); -} \ No newline at end of file +}