Created
August 24, 2022 20:29
-
-
Save spytheman/0d189c5a15e540fcc32a976272bf57c0 to your computer and use it in GitHub Desktop.
A small SDL start field example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdlib.h> | |
#include <SDL.h> | |
#include <experimental/random> | |
#include <vector> | |
const int WIDTH = 1360, HEIGHT = 760; | |
const int H_WIDTH = WIDTH / 2; | |
const int H_HEIGHT = HEIGHT / 2; | |
const int STARS_COUNT = 5000; | |
float speed = 0.1; | |
float radius_delta = 0.001; | |
Uint32 fps_lasttime = SDL_GetTicks(); | |
Uint32 fps_frames = 0; | |
// struct Star ... | |
struct Star { | |
float x, y, z, radius, brightness, viewX, viewY; | |
Star() { newStar(); } | |
void newStar() { | |
x = viewX = std::experimental::randint(0, WIDTH) - H_WIDTH; | |
y = viewY = std::experimental::randint(0, HEIGHT) - H_HEIGHT; | |
z = 256; | |
brightness = 0.0; | |
radius = 1.0; | |
} | |
bool isOffScreen() { | |
return z <= 0 || viewX <= -H_WIDTH || viewY <= -H_HEIGHT || viewX >= H_WIDTH || viewY >= H_HEIGHT; | |
} | |
void processStar() { | |
viewX = std::round(x * 256 / z); | |
viewY = std::round(y * 256 / z); | |
z -= speed; | |
radius += radius_delta; | |
if (isOffScreen()) { newStar(); } | |
if (brightness < 256) { brightness += 0.15; } | |
} | |
}; | |
void setFPStitle(SDL_Window *window) { | |
fps_frames++; | |
if (fps_lasttime < SDL_GetTicks() - 1000) { | |
fps_lasttime = SDL_GetTicks(); | |
auto title = "C++ SDL window: " + std::to_string(fps_frames) + " FPS"; | |
fps_frames = 0; | |
SDL_SetWindowTitle(window, title.c_str()); | |
} | |
} | |
int SDL_RenderDrawCircle(SDL_Renderer * renderer, int x, int y, int radius) { | |
int offsetx, offsety, d; | |
int status; | |
offsetx = 0; | |
offsety = radius; | |
d = radius -1; | |
status = 0; | |
while (offsety >= offsetx) { | |
status += SDL_RenderDrawPoint(renderer, x + offsetx, y + offsety); | |
status += SDL_RenderDrawPoint(renderer, x + offsety, y + offsetx); | |
status += SDL_RenderDrawPoint(renderer, x - offsetx, y + offsety); | |
status += SDL_RenderDrawPoint(renderer, x - offsety, y + offsetx); | |
status += SDL_RenderDrawPoint(renderer, x + offsetx, y - offsety); | |
status += SDL_RenderDrawPoint(renderer, x + offsety, y - offsetx); | |
status += SDL_RenderDrawPoint(renderer, x - offsetx, y - offsety); | |
status += SDL_RenderDrawPoint(renderer, x - offsety, y - offsetx); | |
if (status < 0) { | |
status = -1; | |
break; | |
} | |
if (d >= 2*offsetx) { | |
d -= 2*offsetx + 1; | |
offsetx +=1; | |
} | |
else if (d < 2 * (radius - offsety)) { | |
d += 2 * offsety - 1; | |
offsety -= 1; | |
} | |
else { | |
d += 2 * (offsety - offsetx - 1); | |
offsety -= 1; | |
offsetx += 1; | |
} | |
} | |
return status; | |
} | |
int SDL_RenderFillCircle(SDL_Renderer * renderer, int x, int y, int radius) { | |
int offsetx, offsety, d; | |
int status; | |
offsetx = 0; | |
offsety = radius; | |
d = radius -1; | |
status = 0; | |
while (offsety >= offsetx) { | |
status += SDL_RenderDrawLine(renderer, x - offsety, y + offsetx, x + offsety, y + offsetx); | |
status += SDL_RenderDrawLine(renderer, x - offsetx, y + offsety, x + offsetx, y + offsety); | |
status += SDL_RenderDrawLine(renderer, x - offsetx, y - offsety, x + offsetx, y - offsety); | |
status += SDL_RenderDrawLine(renderer, x - offsety, y - offsetx, x + offsety, y - offsetx); | |
if (status < 0) { | |
status = -1; | |
break; | |
} | |
if (d >= 2*offsetx) { | |
d -= 2*offsetx + 1; | |
offsetx +=1; | |
} | |
else if (d < 2 * (radius - offsety)) { | |
d += 2 * offsety - 1; | |
offsety -= 1; | |
} | |
else { | |
d += 2 * (offsety - offsetx - 1); | |
offsety -= 1; | |
offsetx += 1; | |
} | |
} | |
return status; | |
} | |
int main(int argc, char *argv[]) { | |
SDL_Init(SDL_INIT_EVERYTHING); | |
SDL_Window *window = SDL_CreateWindow("C++ SDL window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, | |
WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI); | |
if (window == NULL) { | |
std::cout << "Could not create window: " << SDL_GetError() << std::endl; | |
return 1; | |
} | |
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0); | |
if (renderer == NULL) { | |
std::cout << "Could not init renderer: " << SDL_GetError() << std::endl; | |
return 1; | |
} | |
// SDL initialization ... | |
SDL_Event windowEvent; | |
std::vector<Star> stars(STARS_COUNT, Star()); | |
while (true) { | |
if (SDL_PollEvent(&windowEvent)) { | |
if (windowEvent.type == SDL_QUIT) break; | |
} | |
setFPStitle(window); | |
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | |
SDL_RenderClear(renderer); | |
for (int i = 0; i < STARS_COUNT; i++) { | |
Star *star = &stars[i]; | |
star->processStar(); | |
SDL_SetRenderDrawColor(renderer, star->brightness, star->brightness, star->brightness, 255); | |
int x = star->viewX + H_WIDTH; | |
int y = star->viewY + H_HEIGHT; | |
SDL_RenderFillCircle(renderer, x, y, std::round(star->radius)); | |
} | |
SDL_RenderPresent(renderer); | |
} | |
SDL_DestroyRenderer(renderer); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment