2015-06-21 23:33:46 +08:00
|
|
|
/*
|
2024-01-02 05:15:26 +08:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
/* Simple program: Check viewports */
|
|
|
|
|
2022-11-27 12:43:38 +08:00
|
|
|
#include <SDL3/SDL_test.h>
|
|
|
|
#include <SDL3/SDL_test_common.h>
|
2022-12-15 12:58:20 +08:00
|
|
|
#include <SDL3/SDL_main.h>
|
2022-04-12 20:07:18 +08:00
|
|
|
#include "testutils.h"
|
2015-06-21 23:33:46 +08:00
|
|
|
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-06-21 23:33:46 +08:00
|
|
|
static SDLTest_CommonState *state;
|
|
|
|
|
2024-06-13 10:08:06 +08:00
|
|
|
static SDL_Rect viewport;
|
2020-03-09 12:02:40 +08:00
|
|
|
static int done, j;
|
2024-09-18 22:52:28 +08:00
|
|
|
static bool use_target = false;
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2020-03-09 12:02:40 +08:00
|
|
|
static Uint32 wait_start;
|
2015-06-21 23:33:46 +08:00
|
|
|
#endif
|
2020-03-09 12:02:40 +08:00
|
|
|
static SDL_Texture *sprite;
|
|
|
|
static int sprite_w, sprite_h;
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
|
|
|
static void
|
|
|
|
quit(int rc)
|
|
|
|
{
|
|
|
|
SDLTest_CommonQuit(state);
|
2023-04-12 17:07:28 +08:00
|
|
|
/* Let 'main()' return normally */
|
|
|
|
if (rc != 0) {
|
|
|
|
exit(rc);
|
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
|
2023-03-08 18:40:07 +08:00
|
|
|
static void DrawOnViewport(SDL_Renderer *renderer)
|
2022-12-01 04:51:59 +08:00
|
|
|
{
|
2023-01-01 03:19:32 +08:00
|
|
|
SDL_FRect rect;
|
2024-06-13 10:08:06 +08:00
|
|
|
SDL_Rect cliprect;
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
/* Set the viewport */
|
2022-12-27 22:21:13 +08:00
|
|
|
SDL_SetRenderViewport(renderer, &viewport);
|
2022-12-01 04:51:59 +08:00
|
|
|
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Draw a gray background */
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xFF);
|
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
|
|
|
|
/* Test inside points */
|
2020-03-09 12:02:40 +08:00
|
|
|
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0x00, 0xFF);
|
2024-06-13 10:08:06 +08:00
|
|
|
SDL_RenderPoint(renderer, (float)(viewport.h / 2 + 20), (float)(viewport.w / 2));
|
|
|
|
SDL_RenderPoint(renderer, (float)(viewport.h / 2 - 20), (float)(viewport.w / 2));
|
|
|
|
SDL_RenderPoint(renderer, (float)(viewport.h / 2), (float)(viewport.w / 2 - 20));
|
|
|
|
SDL_RenderPoint(renderer, (float)(viewport.h / 2), (float)(viewport.w / 2 + 20));
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
/* Test horizontal and vertical lines */
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
|
2024-06-13 10:08:06 +08:00
|
|
|
SDL_RenderLine(renderer, 1.0f, 0.0f, (float)(viewport.w - 2), 0.0f);
|
|
|
|
SDL_RenderLine(renderer, 1.0f, (float)(viewport.h - 1), (float)(viewport.w - 2), (float)(viewport.h - 1));
|
|
|
|
SDL_RenderLine(renderer, 0.0f, 1.0f, 0.0f, (float)(viewport.h - 2));
|
|
|
|
SDL_RenderLine(renderer, (float)(viewport.w - 1), 1.0f, (float)(viewport.w - 1), (float)(viewport.h - 2));
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
/* Test diagonal lines */
|
2023-01-01 03:19:32 +08:00
|
|
|
SDL_SetRenderDrawColor(renderer, 0x00, 0xff, 0xFF, 0xFF);
|
2024-06-13 10:08:06 +08:00
|
|
|
SDL_RenderLine(renderer, 0.0f, 0.0f, (float)(viewport.w - 1), (float)(viewport.h - 1));
|
|
|
|
SDL_RenderLine(renderer, (float)(viewport.w - 1), 0.0f, 0.0f, (float)(viewport.h - 1));
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
/* Test outside points */
|
2020-03-09 12:02:40 +08:00
|
|
|
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0x00, 0xFF);
|
2024-06-13 10:08:06 +08:00
|
|
|
SDL_RenderPoint(renderer, (float)(viewport.h / 2 + viewport.h), (float)(viewport.w / 2));
|
|
|
|
SDL_RenderPoint(renderer, (float)(viewport.h / 2 - viewport.h), (float)(viewport.w / 2));
|
|
|
|
SDL_RenderPoint(renderer, (float)(viewport.h / 2), (float)(viewport.w / 2 - viewport.w));
|
|
|
|
SDL_RenderPoint(renderer, (float)(viewport.h / 2), (float)(viewport.w / 2 + viewport.w));
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
/* Add a box at the top */
|
2023-01-01 03:19:32 +08:00
|
|
|
rect.w = 8.0f;
|
|
|
|
rect.h = 8.0f;
|
2024-06-13 10:08:06 +08:00
|
|
|
rect.x = (float)((viewport.w - rect.w) / 2);
|
2023-01-01 03:19:32 +08:00
|
|
|
rect.y = 0.0f;
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_RenderFillRect(renderer, &rect);
|
2020-03-09 12:02:40 +08:00
|
|
|
|
|
|
|
/* Add a clip rect and fill it with the sprite */
|
2024-10-01 11:40:54 +08:00
|
|
|
cliprect.x = (viewport.w - sprite->w) / 2;
|
|
|
|
cliprect.y = (viewport.h - sprite->h) / 2;
|
|
|
|
cliprect.w = sprite->w;
|
|
|
|
cliprect.h = sprite->h;
|
|
|
|
SDL_RectToFRect(&cliprect, &rect);
|
2023-01-01 03:19:32 +08:00
|
|
|
SDL_SetRenderClipRect(renderer, &cliprect);
|
2022-12-27 22:21:13 +08:00
|
|
|
SDL_RenderTexture(renderer, sprite, NULL, &rect);
|
|
|
|
SDL_SetRenderClipRect(renderer, NULL);
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
|
2023-03-08 18:40:07 +08:00
|
|
|
static void loop(void)
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
2022-11-01 20:10:46 +08:00
|
|
|
SDL_Event event;
|
|
|
|
int i;
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 23:33:46 +08:00
|
|
|
/* Avoid using delays */
|
2022-11-28 00:38:43 +08:00
|
|
|
if (SDL_GetTicks() - wait_start < 1000) {
|
2015-06-21 23:33:46 +08:00
|
|
|
return;
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
wait_start = SDL_GetTicks();
|
|
|
|
#endif
|
|
|
|
/* Check for events */
|
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
|
SDLTest_CommonEvent(state, &event, &done);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move a viewport box in steps around the screen */
|
|
|
|
viewport.x = j * 100;
|
|
|
|
viewport.y = viewport.x;
|
|
|
|
viewport.w = 100 + j * 50;
|
|
|
|
viewport.h = 100 + j * 50;
|
|
|
|
j = (j + 1) % 4;
|
2024-06-13 10:08:06 +08:00
|
|
|
SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
2022-11-28 00:38:43 +08:00
|
|
|
if (state->windows[i] == NULL) {
|
2015-06-21 23:33:46 +08:00
|
|
|
continue;
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
/* Draw using viewport */
|
2021-03-27 06:52:30 +08:00
|
|
|
DrawOnViewport(state->renderers[i]);
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
/* Update the screen! */
|
|
|
|
if (use_target) {
|
|
|
|
SDL_SetRenderTarget(state->renderers[i], NULL);
|
2022-12-27 22:21:13 +08:00
|
|
|
SDL_RenderTexture(state->renderers[i], state->targets[i], NULL, NULL);
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_RenderPresent(state->renderers[i]);
|
|
|
|
SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
|
|
|
|
} else {
|
|
|
|
SDL_RenderPresent(state->renderers[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 23:33:46 +08:00
|
|
|
if (done) {
|
|
|
|
emscripten_cancel_main_loop();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-12-01 04:51:59 +08:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
|
|
|
int i;
|
2022-12-02 17:17:17 +08:00
|
|
|
Uint64 then, now;
|
|
|
|
Uint32 frames;
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
|
2023-11-10 05:29:15 +08:00
|
|
|
if (!state) {
|
2015-06-21 23:33:46 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < argc;) {
|
|
|
|
int consumed;
|
|
|
|
|
|
|
|
consumed = SDLTest_CommonArg(state, i);
|
|
|
|
if (consumed == 0) {
|
|
|
|
consumed = -1;
|
|
|
|
if (SDL_strcasecmp(argv[i], "--target") == 0) {
|
2024-09-18 22:52:28 +08:00
|
|
|
use_target = true;
|
2015-06-21 23:33:46 +08:00
|
|
|
consumed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (consumed < 0) {
|
2019-05-29 05:39:13 +08:00
|
|
|
static const char *options[] = { "[--target]", NULL };
|
|
|
|
SDLTest_CommonLogUsage(state, argv[0], options);
|
2015-06-21 23:33:46 +08:00
|
|
|
quit(1);
|
|
|
|
}
|
|
|
|
i += consumed;
|
|
|
|
}
|
|
|
|
if (!SDLTest_CommonInit(state)) {
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
|
2024-09-18 22:52:28 +08:00
|
|
|
sprite = LoadTexture(state->renderers[0], "icon.bmp", true, &sprite_w, &sprite_h);
|
2022-04-12 20:07:18 +08:00
|
|
|
|
2023-11-10 05:29:15 +08:00
|
|
|
if (!sprite) {
|
2020-03-09 12:02:40 +08:00
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
|
2015-06-21 23:33:46 +08:00
|
|
|
if (use_target) {
|
|
|
|
int w, h;
|
|
|
|
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
SDL_GetWindowSize(state->windows[i], &w, &h);
|
|
|
|
state->targets[i] = SDL_CreateTexture(state->renderers[i], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
|
|
|
|
SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
SDL_Renderer *renderer = state->renderers[i];
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
|
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Main render loop */
|
|
|
|
frames = 0;
|
|
|
|
then = SDL_GetTicks();
|
|
|
|
done = 0;
|
|
|
|
j = 0;
|
|
|
|
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 23:33:46 +08:00
|
|
|
wait_start = SDL_GetTicks();
|
|
|
|
emscripten_set_main_loop(loop, 0, 1);
|
|
|
|
#else
|
|
|
|
while (!done) {
|
|
|
|
++frames;
|
|
|
|
loop();
|
|
|
|
SDL_Delay(1000);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Print out some timing information */
|
|
|
|
now = SDL_GetTicks();
|
|
|
|
if (now > then) {
|
2022-12-01 04:51:59 +08:00
|
|
|
double fps = ((double)frames * 1000) / (now - then);
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_Log("%2.2f frames per second\n", fps);
|
|
|
|
}
|
|
|
|
quit(0);
|
|
|
|
return 0;
|
|
|
|
}
|