Skip to content

Instantly share code, notes, and snippets.

@alexmaryin
Created May 5, 2022 13:24
Show Gist options
  • Save alexmaryin/2f7ad54aacc076e7e322ff53eac158d3 to your computer and use it in GitHub Desktop.
Save alexmaryin/2f7ad54aacc076e7e322ff53eac158d3 to your computer and use it in GitHub Desktop.
Starfield C++ main
// imports ...
const int WIDTH = 1360, HEIGHT = 760;
const int H_WIDTH = WIDTH / 2;
const int H_HEIGHT = HEIGHT / 2;
const int STARS_COUNT = 200;
float speed = 0.1;
float radius_delta = 0.001;
Uint32 fps_lasttime = SDL_GetTicks();
Uint32 fps_frames = 0;
// struct Star ...
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 main(int argc, char *argv[])
{
// 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