Skip to content

Instantly share code, notes, and snippets.

@HarryR
Created December 12, 2012 01:58
Show Gist options
  • Select an option

  • Save HarryR/4264199 to your computer and use it in GitHub Desktop.

Select an option

Save HarryR/4264199 to your computer and use it in GitHub Desktop.
Playing with tiled triangles in different shades of grey.
#include "SDL.h"
#include "SDL_gfxPrimitives.h"
#include "SDL_framerate.h"
#include <stdbool.h>
#include <math.h>
static const int laycnt = 7;
static int layalpha[7];
static double lay[7];
void
draw_triangles( SDL_Surface *surface, int x1, int y1, int x2, int y2, int tri_width, int tri_height ) {
int i, sx, sy;
i = 0;
sy = y1 - tri_height;
while( sy < y2 ) {
sx = x1 - tri_width;
while( sx <= x2 ) {
int middle_point_x = sx + (tri_width / 2);
int lcorner_x = sx;
if( i % 2 == 0 ) {
/* Point at bottom */
int middle_point_y = sy + ((tri_height / 3));
int lcorner_y = sy;
int rcorner_x = sx + tri_width;
int rcorner_y = sy;
int pcorner_y = sy + tri_height;
filledTrigonRGBA(surface,
lcorner_x, lcorner_y,
rcorner_x, rcorner_y,
middle_point_x, middle_point_y,
255, 255, 255, layalpha[1]);
filledTrigonRGBA(surface,
lcorner_x, lcorner_y,
middle_point_x, middle_point_y,
middle_point_x, pcorner_y,
255, 255, 255, layalpha[2]);
filledTrigonRGBA(surface,
rcorner_x, rcorner_y,
middle_point_x, middle_point_y,
middle_point_x, pcorner_y,
255, 255, 255, layalpha[3]);
}
else {
/* Point at top */
int middle_point_y = sy + ((tri_height / 3) * 2);
int lcorner_y = sy + tri_height;
int rcorner_x = sx + tri_width;
int rcorner_y = sy + tri_height;
int pcorner_y = sy;
filledTrigonRGBA(surface,
lcorner_x, lcorner_y,
rcorner_x, rcorner_y,
middle_point_x, middle_point_y,
255, 255, 255, layalpha[4]);
filledTrigonRGBA(surface,
lcorner_x, lcorner_y,
middle_point_x, middle_point_y,
middle_point_x, pcorner_y,
255, 255, 255, layalpha[5]);
filledTrigonRGBA(surface,
rcorner_x, rcorner_y,
middle_point_x, middle_point_y,
middle_point_x, pcorner_y,
255, 255, 255, layalpha[6]);
}
i++;
sx += (tri_width / 2);
}
sy += tri_height;
}
}
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720
int
main( int argc, char **argv ) {
int i;
SDL_Init( SDL_INIT_VIDEO );
SDL_Surface *screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
SDL_WM_SetCaption( "Moar Triangles", 0 );
SDL_Event event;
FPSmanager framerate;
bool is_running = true;
for( i = 0; i < laycnt; i++ ) {
lay[i] = 0;
}
SDL_initFramerate(&framerate);
SDL_setFramerate(&framerate, 30);
while( is_running ) {
if( SDL_PollEvent(&event) ) {
if( event.type == SDL_QUIT ) {
is_running = false;
}
else if( event.type == SDL_KEYDOWN ) {
if( event.key.keysym.sym == SDLK_ESCAPE ) {
is_running = false;
}
}
}
for( i = 0; i < laycnt; i++ ) {
lay[i] += i;
layalpha[i] = 50 + fabs(sin(1 + (lay[i] / 300))) * 200;
}
SDL_FillRect( screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0) );
draw_triangles(screen, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 60, 40);
SDL_Flip( screen );
SDL_framerateDelay(&framerate);
}
SDL_Quit();
return 0;
}
@HarryR

HarryR commented Dec 12, 2012

Copy link
Copy Markdown
Author

It creates many interesting geometric patterns

Patterns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment