Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL3/SDL.h>
- #include <SDL3_image/SDL_image.h>
- #include <iostream>
- #include "ntl_bmp.h"
- int main() {
- if (!SDL_Init(SDL_INIT_VIDEO)) {
- return 1;
- }
- SDL_Window *window = SDL_CreateWindow(
- "NTL Screensaver",
- 1280, 720,
- 0
- // SDL_WINDOW_FULLSCREEN
- );
- SDL_Renderer *renderer =
- SDL_CreateRenderer(window, nullptr);
- static SDL_Texture *texture = nullptr;
- texture = IMG_LoadTexture_IO(renderer, SDL_IOFromConstMem(ntl_bmp, ntl_bmp_len), true);
- if (!texture) {
- SDL_Log("Couldn't load icon: %s\n", SDL_GetError());
- return SDL_APP_FAILURE;
- }
- bool running = true;
- SDL_FRect logo = {
- 0.0f, // x
- 0.0f, // y
- 259.0f, // width
- 259.0f // height
- };
- constexpr float step = 700.0f; // pixels per second
- Uint64 previousTime = SDL_GetTicksNS();
- float moveY = step;
- float moveX = step;
- int windowWidth;
- int windowHeight;
- // Retrieve the actual size in physical pixels
- SDL_GetWindowSizeInPixels(window, &windowWidth, &windowHeight);
- while (running) {
- // Delta Time
- const Uint64 currentTime = SDL_GetTicksNS();
- const float deltaTime =
- static_cast<float>(currentTime - previousTime) / 1000000000.0f;
- previousTime = currentTime;
- // Events
- SDL_Event event;
- while (SDL_PollEvent(&event)) {
- if (event.type == SDL_EVENT_QUIT) {
- running = false;
- }
- if (event.type == SDL_EVENT_KEY_DOWN) {
- if (event.key.key == SDLK_ESCAPE) {
- running = false;
- }
- }
- }
- // Next move
- const float nextMoveX = (moveX * deltaTime);
- const float nextMoveY = (moveY * deltaTime);
- // Evaluate next move
- const bool outOfBoundsRight = logo.x + logo.w + nextMoveX >= windowWidth;
- const bool outOfBoundsBottom = logo.y + logo.h + nextMoveY >= windowHeight;
- const bool outOfBoundsLeft = logo.x + nextMoveX <= 0;
- const bool outOfBoundsTop = logo.y + nextMoveY <= 0;
- // Detect Collisions
- // Y axis
- if (outOfBoundsBottom) { // hits down moving right or left
- moveY = -step;
- } else if (outOfBoundsTop) { // hits up moving right or left
- moveY = step;
- }
- // X axis
- if (outOfBoundsRight) { // hit right moving up or down
- moveX = -step;
- } else if (outOfBoundsLeft) {
- moveX = step; // hit left moving up or down
- }
- // If we hit any corner (this seems too busy not sure)
- if ((outOfBoundsRight && outOfBoundsBottom) || (
- outOfBoundsLeft && outOfBoundsTop || outOfBoundsRight && outOfBoundsTop || outOfBoundsLeft &&
- outOfBoundsBottom)) {
- std::cout << "Hit corner" << std::endl;
- }
- // Move
- logo.x += moveX * deltaTime;
- logo.y += moveY * deltaTime;
- // Rendering
- // Clear screen with black
- SDL_SetRenderDrawColor(
- renderer,
- 0,
- 0,
- 0,
- 255
- );
- SDL_RenderClear(renderer);
- // Draw logo
- SDL_RenderTexture(renderer, texture, nullptr, &logo);
- SDL_RenderPresent(renderer);
- }
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment