Guest User

Bouncy Screensaver C++ SDL3

a guest
Oct 25th, 2026
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | Source Code | 0 0
  1. #include <SDL3/SDL.h>
  2. #include <SDL3_image/SDL_image.h>
  3. #include <iostream>
  4. #include "ntl_bmp.h"
  5.  
  6. int main() {
  7.  
  8.     if (!SDL_Init(SDL_INIT_VIDEO)) {
  9.         return 1;
  10.     }
  11.  
  12.     SDL_Window *window = SDL_CreateWindow(
  13.         "NTL Screensaver",
  14.         1280, 720,
  15.         0
  16.         // SDL_WINDOW_FULLSCREEN
  17.     );
  18.  
  19.     SDL_Renderer *renderer =
  20.             SDL_CreateRenderer(window, nullptr);
  21.  
  22.     static SDL_Texture *texture = nullptr;
  23.  
  24.     texture = IMG_LoadTexture_IO(renderer, SDL_IOFromConstMem(ntl_bmp, ntl_bmp_len), true);
  25.     if (!texture) {
  26.         SDL_Log("Couldn't load icon: %s\n", SDL_GetError());
  27.         return SDL_APP_FAILURE;
  28.     }
  29.     bool running = true;
  30.  
  31.     SDL_FRect logo = {
  32.         0.0f,       // x
  33.         0.0f,       // y
  34.         259.0f,     // width
  35.         259.0f      // height
  36.     };
  37.  
  38.     constexpr float step = 700.0f; // pixels per second
  39.  
  40.     Uint64 previousTime = SDL_GetTicksNS();
  41.  
  42.     float moveY = step;
  43.     float moveX = step;
  44.  
  45.     int windowWidth;
  46.     int windowHeight;
  47.  
  48.     // Retrieve the actual size in physical pixels
  49.     SDL_GetWindowSizeInPixels(window, &windowWidth, &windowHeight);
  50.  
  51.     while (running) {
  52.  
  53.         // Delta Time
  54.         const Uint64 currentTime = SDL_GetTicksNS();
  55.  
  56.         const float deltaTime =
  57.                 static_cast<float>(currentTime - previousTime) / 1000000000.0f;
  58.  
  59.         previousTime = currentTime;
  60.  
  61.  
  62.         // Events
  63.         SDL_Event event;
  64.  
  65.         while (SDL_PollEvent(&event)) {
  66.             if (event.type == SDL_EVENT_QUIT) {
  67.                 running = false;
  68.             }
  69.  
  70.             if (event.type == SDL_EVENT_KEY_DOWN) {
  71.                 if (event.key.key == SDLK_ESCAPE) {
  72.                     running = false;
  73.                 }
  74.             }
  75.         }
  76.  
  77.         // Next move
  78.         const float nextMoveX = (moveX * deltaTime);
  79.         const float nextMoveY = (moveY * deltaTime);
  80.  
  81.         // Evaluate next move
  82.         const bool outOfBoundsRight = logo.x + logo.w + nextMoveX >= windowWidth;
  83.         const bool outOfBoundsBottom = logo.y + logo.h + nextMoveY >= windowHeight;
  84.         const bool outOfBoundsLeft = logo.x + nextMoveX <= 0;
  85.         const bool outOfBoundsTop = logo.y + nextMoveY <= 0;
  86.  
  87.  
  88.         // Detect Collisions
  89.  
  90.         // Y axis
  91.         if (outOfBoundsBottom) { // hits down moving right or left
  92.             moveY = -step;
  93.         } else if (outOfBoundsTop) { // hits up moving right or left
  94.             moveY = step;
  95.         }
  96.  
  97.         // X axis
  98.         if (outOfBoundsRight) { // hit right moving up or down
  99.             moveX = -step;
  100.         } else if (outOfBoundsLeft) {
  101.             moveX = step; // hit left moving up or down
  102.         }
  103.  
  104.         // If we hit any corner (this seems too busy not sure)
  105.         if ((outOfBoundsRight && outOfBoundsBottom) || (
  106.                 outOfBoundsLeft && outOfBoundsTop || outOfBoundsRight && outOfBoundsTop || outOfBoundsLeft &&
  107.                 outOfBoundsBottom)) {
  108.             std::cout << "Hit corner" << std::endl;
  109.         }
  110.  
  111.         // Move
  112.         logo.x += moveX * deltaTime;
  113.         logo.y += moveY * deltaTime;
  114.  
  115.  
  116.         // Rendering
  117.  
  118.         // Clear screen with black
  119.         SDL_SetRenderDrawColor(
  120.             renderer,
  121.             0,
  122.             0,
  123.             0,
  124.             255
  125.         );
  126.         SDL_RenderClear(renderer);
  127.  
  128.         // Draw logo
  129.         SDL_RenderTexture(renderer, texture, nullptr, &logo);
  130.         SDL_RenderPresent(renderer);
  131.     }
  132.  
  133.     SDL_DestroyRenderer(renderer);
  134.     SDL_DestroyWindow(window);
  135.     SDL_Quit();
  136.  
  137.     return 0;
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment