Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions coresdk/src/test/unit_tests/unit_test_graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

#include "catch.hpp"

#include <cstdio>
#include "graphics.h"
#include "images.h"
#include "utility_functions.h"
#include "resources.h"
#include "window_manager.h"

using namespace splashkit_lib;
Expand All @@ -21,3 +25,126 @@ TEST_CASE("screen dimension utilities", "[screen_width][screen_height]")
// close_window(w);
// }
}

TEST_CASE("can create, save, and load a bitmap", "[bitmap][save][load][get_pixel]")
{
// Create a small 5x5 bitmap
string testBitmapName = "test_bitmap";
bitmap test_bmp = create_bitmap(testBitmapName, 5, 5);

// Fill each row with a different color pattern
// Row 0: WHITE
fill_rectangle_on_bitmap(test_bmp, color_white(), 0, 0, 5, 1);
// Row 1: RED
fill_rectangle_on_bitmap(test_bmp, color_red(), 0, 1, 5, 1);
// Row 2: GREEN
fill_rectangle_on_bitmap(test_bmp, color_green(), 0, 2, 5, 1);
// Row 3: BLUE
fill_rectangle_on_bitmap(test_bmp, color_blue(), 0, 3, 5, 1);
// Row 4: BLACK
fill_rectangle_on_bitmap(test_bmp, color_black(), 0, 4, 5, 1);

SECTION("can create valid bitmap")
{
REQUIRE(has_bitmap(testBitmapName));
REQUIRE(bitmap_width(test_bmp) != 0);
}

SECTION("bitmap displays pixels correctly")
{
// Verify pixels directly from the created bitmap (before save/load)
// Check a pixel from row 0 (WHITE)
color pixel_w0 = get_pixel(test_bmp, 0, 0);
REQUIRE(red_of(pixel_w0) == red_of(color_white()));
REQUIRE(green_of(pixel_w0) == green_of(color_white()));
REQUIRE(blue_of(pixel_w0) == blue_of(color_white()));

// Check a pixel from row 1 (RED)
color pixel_r1 = get_pixel(test_bmp, 2, 1);
REQUIRE(red_of(pixel_r1) == red_of(color_red()));
REQUIRE(green_of(pixel_r1) == green_of(color_red()));
REQUIRE(blue_of(pixel_r1) == blue_of(color_red()));

// Check a pixel from row 2 (GREEN)
color pixel_g2 = get_pixel(test_bmp, 1, 2);
REQUIRE(red_of(pixel_g2) == red_of(color_green()));
REQUIRE(green_of(pixel_g2) == green_of(color_green()));
REQUIRE(blue_of(pixel_g2) == blue_of(color_green()));

// Check a pixel from row 3 (BLUE)
color pixel_b3 = get_pixel(test_bmp, 3, 3);
REQUIRE(red_of(pixel_b3) == red_of(color_blue()));
REQUIRE(green_of(pixel_b3) == green_of(color_blue()));
REQUIRE(blue_of(pixel_b3) == blue_of(color_blue()));

// Check a pixel from row 4 (BLACK)
color pixel_k4 = get_pixel(test_bmp, 4, 4);
REQUIRE(red_of(pixel_k4) == red_of(color_black()));
REQUIRE(green_of(pixel_k4) == green_of(color_black()));
REQUIRE(blue_of(pixel_k4) == blue_of(color_black()));
}

SECTION("can free a bitmap")
{
free_bitmap(test_bmp);
REQUIRE(has_bitmap(testBitmapName) == false);
}

SECTION("can save and load a bitmap")
{
// Save the bitmap to disk
string filename = "test_bitmap_pattern_5x5";
save_bitmap(test_bmp, filename);

// Get the path to the saved bitmap (should be desktop normally)
string path = path_from({path_to_user_home(), "Desktop"});

// Fall back to home if Desktop doesn't exist (mirrors save_bitmap functionality)
if (!directory_exists(path))
{
path = path_to_user_home();
}

string filepath = path + "/" + filename + ".png";

// Load the saved bitmap back
bitmap loaded_bmp = load_bitmap("loaded_test", filepath);

// Clean up the unneeded file
int result = std::remove(filepath.c_str());

// Ensure the bitmap was loaded
REQUIRE(has_bitmap("loaded_test"));
REQUIRE(bitmap_width(loaded_bmp) != 0);

REQUIRE(bitmap_width(loaded_bmp) == 5);
REQUIRE(bitmap_height(loaded_bmp) == 5);

// Verify sample pixels from the loaded bitmap
color loaded_pixel_w = get_pixel(loaded_bmp, 0, 0);
REQUIRE(red_of(loaded_pixel_w) == red_of(color_white()));
REQUIRE(green_of(loaded_pixel_w) == green_of(color_white()));
REQUIRE(blue_of(loaded_pixel_w) == blue_of(color_white()));

color loaded_pixel_r = get_pixel(loaded_bmp, 2, 1);
REQUIRE(red_of(loaded_pixel_r) == red_of(color_red()));
REQUIRE(green_of(loaded_pixel_r) == green_of(color_red()));
REQUIRE(blue_of(loaded_pixel_r) == blue_of(color_red()));

color loaded_pixel_g = get_pixel(loaded_bmp, 1, 2);
REQUIRE(red_of(loaded_pixel_g) == red_of(color_green()));
REQUIRE(green_of(loaded_pixel_g) == green_of(color_green()));
REQUIRE(blue_of(loaded_pixel_g) == blue_of(color_green()));

color loaded_pixel_b = get_pixel(loaded_bmp, 3, 3);
REQUIRE(red_of(loaded_pixel_b) == red_of(color_blue()));
REQUIRE(green_of(loaded_pixel_b) == green_of(color_blue()));
REQUIRE(blue_of(loaded_pixel_b) == blue_of(color_blue()));

color loaded_pixel_k = get_pixel(loaded_bmp, 4, 4);
REQUIRE(red_of(loaded_pixel_k) == red_of(color_black()));
REQUIRE(green_of(loaded_pixel_k) == green_of(color_black()));
REQUIRE(blue_of(loaded_pixel_k) == blue_of(color_black()));
}
free_all_bitmaps();
}