Skip to content

Instantly share code, notes, and snippets.

@Cheeseness
Last active April 9, 2016 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cheeseness/4307fe57db443d09c2f2fa0dd539e3bc to your computer and use it in GitHub Desktop.
Save Cheeseness/4307fe57db443d09c2f2fa0dd539e3bc to your computer and use it in GitHub Desktop.
#include <SDL2/SDL.h>
#include <stdbool.h>
#include <stdio.h>
SDL_Window * window;
SDL_Renderer * renderer;
float get_exp_easing(float ticks, int startSize, int endSize, float duration)
{
printf("%f, ", ticks);
float y = endSize * pow( 2, 10 * (ticks / duration - 1) ) + startSize;
printf("%f\n", y);
return y;
}
void set_pixel(SDL_Renderer *rend, int x, int y, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
SDL_SetRenderDrawColor(rend, r,g,b,a);
SDL_RenderDrawPoint(rend, x, y);
}
void draw_circle(SDL_Renderer *surface, int n_cx, int n_cy, int radius, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
// if the first pixel in the screen is represented by (0,0) (which is in sdl)
// remember that the beginning of the circle is not in the middle of the pixel
// but to the left-top from it:
double error = (double)-radius;
double x = (double)radius - 0.5;
double y = (double)0.5;
double cx = n_cx - 0.5;
double cy = n_cy - 0.5;
while (x >= y)
{
set_pixel(surface, (int)(cx + x), (int)(cy + y), r, g, b, a);
set_pixel(surface, (int)(cx + y), (int)(cy + x), r, g, b, a);
if (x != 0)
{
set_pixel(surface, (int)(cx - x), (int)(cy + y), r, g, b, a);
set_pixel(surface, (int)(cx + y), (int)(cy - x), r, g, b, a);
}
if (y != 0)
{
set_pixel(surface, (int)(cx + x), (int)(cy - y), r, g, b, a);
set_pixel(surface, (int)(cx - y), (int)(cy + x), r, g, b, a);
}
if (x != 0 && y != 0)
{
set_pixel(surface, (int)(cx - x), (int)(cy - y), r, g, b, a);
set_pixel(surface, (int)(cx - y), (int)(cy - x), r, g, b, a);
}
error += y;
++y;
error += y;
if (error >= 0)
{
--x;
error -= x;
error -= x;
}
/*
// sleep for debug
SDL_RenderPresent(gRenderer);
std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
*/
}
}
int main(int argc, char* argv[])
{
SDL_CreateWindowAndRenderer(640, 640, 0, &window, &renderer);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest"); // To get nice 'pixely' upscaling
SDL_RenderSetLogicalSize(renderer, 64, 64);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
int ticks = SDL_GetTicks();
int maxCircleDuration = 2500; //Length of time it takes for the circle to reach full size
int maxCircles = 6; //Number of total circles onscreen at once
bool running = true;
while (ticks < 10000 && running)
{
//Poll for events to prevent Gnome from thinking we've locked up
SDL_Event e;
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
{
running = false;
break;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
for (int i = 0; i < maxCircles; ++i)
{
int temp = ticks - i * (maxCircleDuration / maxCircles);
if (temp > maxCircleDuration) //If the circle has reached its maximum size, reset it back to zero
{
temp = temp % maxCircleDuration;
}
draw_circle(renderer, 32, 32, get_exp_easing(temp, 0, 64, maxCircleDuration), 0, 255, 0, get_exp_easing(temp, 0, 255, maxCircleDuration));
}
SDL_RenderPresent(renderer);
ticks = SDL_GetTicks();
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment