Skip to content

Commit

Permalink
add getters for screen (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
oclyke authored Nov 24, 2023
1 parent deb691e commit 06a1307
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/sicgl/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ int screen_set_corners(
int screen_set_extent(screen_t* screen, ext_t width, ext_t height);
int screen_set_location(screen_t* screen, ext_t lu, ext_t lv);

int screen_get_num_pixels(screen_t* screen, uext_t* num_pixels);
int screen_get_extent(screen_t* screen, ext_t* width, ext_t* height);
int screen_get_location(screen_t* screen, ext_t* lu, ext_t* lv);

int screen_normalize(screen_t* screen);
int screen_intersect(screen_t* target, screen_t* s0, screen_t* s1);

Expand Down
74 changes: 74 additions & 0 deletions src/screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,80 @@ int screen_set_location(screen_t* screen, ext_t lu, ext_t lv) {
return ret;
}

/**
* @brief Get the number of pixels in a screen.
*
* @param screen
* @param num_pixels
* @return int
*/
int screen_get_num_pixels(screen_t* screen, uext_t* num_pixels) {
int ret = 0;
if (NULL == screen) {
ret = -ENOMEM;
goto out;
}

if (NULL != num_pixels) {
*num_pixels = screen->width * screen->height;
}

out:
return ret;
}

/**
* @brief Get the extent of a screen.
*
* @param screen
* @param width
* @param height
* @return int
*/
int screen_get_extent(screen_t* screen, ext_t* width, ext_t* height) {
int ret = 0;
if ((NULL == screen) || (NULL == width) || (NULL == height)) {
ret = -ENOMEM;
goto out;
}

if (NULL != width) {
*width = screen->width;
}
if (NULL != height) {
*height = screen->height;
}

out:
return ret;
}

/**
* @brief Get the location of a screen in global coordinates.
*
* @param screen
* @param lu
* @param lv
* @return int
*/
int screen_get_location(screen_t* screen, ext_t* lu, ext_t* lv) {
int ret = 0;
if ((NULL == screen) || (NULL == lu) || (NULL == lv)) {
ret = -ENOMEM;
goto out;
}

if (NULL != lu) {
*lu = screen->lu;
}
if (NULL != lv) {
*lv = screen->lv;
}

out:
return ret;
}

/**
* @brief Normalize a screen for subsequent operations.
* This function enforces assumptions about screen definition.
Expand Down
104 changes: 104 additions & 0 deletions test/tests/screen/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,107 @@ void test_screen_clip_line(void) {
// this area for cleanup of dynamically allocated items
}

void test_screen_get_num_pixels(void) {
int ret = 0;
screen_t screen;
screen_set(&screen, 0, 0, 0, 0, 0, 0);
uext_t num_pixels = 0;
if (TEST_PROTECT()) {
// check null inputs
ret = screen_get_num_pixels(NULL, &num_pixels);
TEST_ASSERT_LESS_THAN_INT32_MESSAGE(
0, ret, "should return negative error code");

// 3x3 screen
ret = screen_set(&screen, 0, 0, 2, 2, 0, 0);
TEST_ASSERT_EQUAL_INT(0, ret);

// test
ret = screen_get_num_pixels(&screen, &num_pixels);
TEST_ASSERT_EQUAL_INT(0, ret);
TEST_ASSERT_EQUAL_INT(9, num_pixels);

// 8x8 screen
ret = screen_set(&screen, 0, 0, 7, 7, 0, 0);
TEST_ASSERT_EQUAL_INT(0, ret);

// test
ret = screen_get_num_pixels(&screen, &num_pixels);
TEST_ASSERT_EQUAL_INT(0, ret);
TEST_ASSERT_EQUAL_INT(64, num_pixels);
} else {
// this area for cleanup of dynamically allocated items
}
}

void test_screen_get_extent(void) {
int ret = 0;
screen_t screen;
screen_set(&screen, 0, 0, 0, 0, 0, 0);
ext_t width = 0;
ext_t height = 0;
if (TEST_PROTECT()) {
// check null inputs
ret = screen_get_extent(NULL, &width, &height);
TEST_ASSERT_LESS_THAN_INT32_MESSAGE(
0, ret, "should return negative error code");

// 3x3 screen
ret = screen_set(&screen, 0, 0, 2, 2, 0, 0);
TEST_ASSERT_EQUAL_INT(0, ret);

ret = screen_get_extent(&screen, &width, &height);
TEST_ASSERT_EQUAL_INT(0, ret);
TEST_ASSERT_EQUAL_INT(3, width);
TEST_ASSERT_EQUAL_INT(3, height);

// 8x8 screen
ret = screen_set(&screen, 0, 0, 7, 7, 0, 0);
TEST_ASSERT_EQUAL_INT(0, ret);

ret = screen_get_extent(&screen, &width, &height);
TEST_ASSERT_EQUAL_INT(0, ret);
TEST_ASSERT_EQUAL_INT(8, width);
TEST_ASSERT_EQUAL_INT(8, height);
} else {
// this area for cleanup of dynamically allocated items
}
}

void test_screen_get_location(void) {
int ret = 0;
screen_t screen;
screen_set(&screen, 0, 0, 0, 0, 0, 0);
ext_t lu = 0;
ext_t lv = 0;
if (TEST_PROTECT()) {
// check null inputs
ret = screen_get_location(NULL, &lu, &lv);
TEST_ASSERT_LESS_THAN_INT32_MESSAGE(
0, ret, "should return negative error code");

// at (0, 0)
ret = screen_set(&screen, 0, 0, 2, 2, 0, 0);
TEST_ASSERT_EQUAL_INT(0, ret);

ret = screen_get_location(&screen, &lu, &lv);
TEST_ASSERT_EQUAL_INT(0, ret);
TEST_ASSERT_EQUAL_INT(0, lu);
TEST_ASSERT_EQUAL_INT(0, lv);

// at (3, 3)
ret = screen_set(&screen, 0, 0, 7, 7, 3, 3);
TEST_ASSERT_EQUAL_INT(0, ret);

ret = screen_get_location(&screen, &lu, &lv);
TEST_ASSERT_EQUAL_INT(0, ret);
TEST_ASSERT_EQUAL_INT(3, lu);
TEST_ASSERT_EQUAL_INT(3, lv);
} else {
// this area for cleanup of dynamically allocated items
}
}

// not needed when using generate_test_runner.rb
int main(void) {
UNITY_BEGIN();
Expand All @@ -533,5 +634,8 @@ int main(void) {
RUN_TEST(test_screen_clip_vline);
RUN_TEST(test_screen_clip_diagonal);
RUN_TEST(test_screen_clip_line);
RUN_TEST(test_screen_get_num_pixels);
RUN_TEST(test_screen_get_extent);
RUN_TEST(test_screen_get_location);
return UNITY_END();
}

0 comments on commit 06a1307

Please sign in to comment.