Initial fork
This commit is contained in:
commit
059a2ba2a1
|
@ -0,0 +1,46 @@
|
|||
## SDL2 playground
|
||||
|
||||
### Phyllotaxis
|
||||
|
||||
![Phyllotaxis](http://i.imgur.com/GrhErt6.png)
|
||||
|
||||
### Random walker
|
||||
|
||||
M changes the blending mode to mod.
|
||||
|
||||
N changes the blending mode to None.
|
||||
|
||||
B changes the blending mode to Blend.
|
||||
|
||||
A changes the blending mode to Add.
|
||||
|
||||
C clears the canvas.
|
||||
|
||||
L draws random lines.
|
||||
|
||||
![Rndwlkr](https://i.imgur.com/Tbt1vf5.png)
|
||||
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
distribute this software, either in source code form or as a compiled
|
||||
binary, for any purpose, commercial or non-commercial, and by any
|
||||
means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors
|
||||
of this software dedicate any and all copyright interest in the
|
||||
software to the public domain. We make this dedication for the benefit
|
||||
of the public at large and to the detriment of our heirs and
|
||||
successors. We intend this dedication to be an overt act of
|
||||
relinquishment in perpetuity of all present and future rights to this
|
||||
software under copyright law.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
For more information, please refer to <http://unlicense.org/>
|
|
@ -0,0 +1,93 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <inttypes.h>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#ifdef SKIP
|
||||
clang -g -Wall -Wextra -Wpedantic --std=gnu99 -O3 \
|
||||
`pkg-config --cflags sdl2` \
|
||||
-o phyllotaxis phyllotaxis.c \
|
||||
`pkg-config --libs sdl2` -lm
|
||||
exit 0
|
||||
#endif
|
||||
|
||||
void star(SDL_Renderer *r, uint16_t x, uint16_t y ){
|
||||
SDL_RenderDrawPoint(r, x, y);
|
||||
SDL_RenderDrawPoint(r, x, y+1);
|
||||
SDL_RenderDrawPoint(r, x, y-1);
|
||||
SDL_RenderDrawPoint(r, x-1, y);
|
||||
SDL_RenderDrawPoint(r, x+1, y);
|
||||
}
|
||||
|
||||
int main(){
|
||||
/* defs */
|
||||
SDL_Init(SDL_INIT_EVERYTHING);
|
||||
char * appname = "phyllotaxis";
|
||||
SDL_Window *w;
|
||||
SDL_Renderer *r;
|
||||
SDL_Texture *t;
|
||||
SDL_Event e;
|
||||
uint16_t c, n, x, y;
|
||||
float rad, a;
|
||||
uint16_t ww = 1366;
|
||||
uint16_t wh = 768;
|
||||
n = 0;
|
||||
c = 4;
|
||||
|
||||
/* inits */
|
||||
w = SDL_CreateWindow(appname, SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED, ww, wh, 0x0);
|
||||
r = SDL_CreateRenderer(w, -1,
|
||||
SDL_RENDERER_ACCELERATED |
|
||||
SDL_RENDERER_TARGETTEXTURE |
|
||||
SDL_RENDERER_PRESENTVSYNC);
|
||||
t = SDL_CreateTexture(r, SDL_PIXELFORMAT_RGBA4444,
|
||||
SDL_TEXTUREACCESS_TARGET, ww, wh);
|
||||
|
||||
/* Set canvas to be the texture t */
|
||||
SDL_SetRenderTarget(r, t);
|
||||
/* clear it */
|
||||
SDL_SetRenderDrawColor(r, 0x00, 0x00, 0x00, 0xF0);
|
||||
SDL_RenderClear(r);
|
||||
|
||||
do{
|
||||
SDL_PollEvent(&e);
|
||||
/* CTRL+Q quits */
|
||||
if((e.key.keysym.sym == SDLK_q) &
|
||||
(SDL_GetModState() == KMOD_LCTRL)) break;
|
||||
n++;
|
||||
|
||||
/* Set actual canvas to be the renderer r */
|
||||
SDL_SetRenderTarget(r, 0x00);
|
||||
/* clear it */
|
||||
SDL_SetRenderDrawColor(r, 0x00, 0x00, 0x00, 0xF0);
|
||||
SDL_RenderClear(r);
|
||||
/* Set the actual canvas to be the texture t */
|
||||
SDL_SetRenderTarget(r, t);
|
||||
|
||||
/* draw stuff */
|
||||
a = (180/M_PI) * n * 137.3;
|
||||
rad = c * sqrt(n);
|
||||
|
||||
x = (uint16_t) rad * cosf(a) + ww/2;
|
||||
y = (uint16_t) rad * sinf(a) + wh/2;
|
||||
SDL_SetRenderDrawColor(r, (int)n%256, (int)a%256, (int)rad%256, 0xF0);
|
||||
if( (0 < x < ww) & (0 < y < wh) )
|
||||
star(r, (int) x, (int) y);
|
||||
|
||||
/* actual canvas is r */
|
||||
SDL_SetRenderTarget(r, 0x00);
|
||||
/* copy t to actual canvas (r) */
|
||||
SDL_RenderCopy(r, t, 0x00, 0x00);
|
||||
/* present canvas r */
|
||||
SDL_RenderPresent(r);
|
||||
/*SDL_Delay(5);*/
|
||||
} while(1);
|
||||
|
||||
/* exit */
|
||||
SDL_DestroyRenderer(r);
|
||||
SDL_DestroyTexture(t);
|
||||
SDL_DestroyWindow(w);
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
}
|
|
@ -0,0 +1,195 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <limits.h>
|
||||
|
||||
#define RND() random()
|
||||
|
||||
#ifdef SKIP
|
||||
: ${CC=gcc}
|
||||
$CC `pkg-config --cflags sdl2` \
|
||||
-o rndwlkr rndwlkr.c \
|
||||
`pkg-config --libs sdl2`
|
||||
exit 0
|
||||
#endif
|
||||
|
||||
struct {
|
||||
uint16_t w;
|
||||
uint16_t h;
|
||||
} std = {
|
||||
.w = 1024,
|
||||
.h = 512
|
||||
};
|
||||
|
||||
int main(){
|
||||
srandom(time(0));
|
||||
|
||||
SDL_Init(SDL_INIT_EVERYTHING);
|
||||
|
||||
SDL_Window * w = SDL_CreateWindow(
|
||||
"SDL",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED, std.w, std.h,
|
||||
0
|
||||
/*
|
||||
| SDL_WINDOW_MAXIMIZED
|
||||
| SDL_WINDOW_FULLSCREEN
|
||||
| SDL_WINDOW_BORDERLESS
|
||||
| SDL_WINDOW_FULLSCREEN_DESKTOP
|
||||
*/
|
||||
);
|
||||
|
||||
SDL_Surface * wicon = SDL_LoadBMP("wicon.bmp");
|
||||
|
||||
SDL_SetWindowIcon(w, wicon);
|
||||
|
||||
/* The surface we should be rendering on
|
||||
SDL_Surface * s = SDL_CreateRGBSurface(
|
||||
0, 512, 256, 8, 0, 0, 0, 0);
|
||||
*/
|
||||
SDL_Surface * s = SDL_GetWindowSurface(w);
|
||||
|
||||
SDL_Renderer * r = SDL_CreateSoftwareRenderer(s);
|
||||
|
||||
printf(
|
||||
"s.w = %u\n"
|
||||
"s.h = %u\n"
|
||||
"s.flags = %u\n"
|
||||
"s.pitch = %u\n"
|
||||
"s.pixels = %p\n"
|
||||
"s.locked = %u\n"
|
||||
"s.format.format = %u\n"
|
||||
"s.format.BitsPerPixel = %u\n"
|
||||
"s.format.BytesPerPixel = %u\n"
|
||||
"SDL_GetNumVideoDrivers = %d\n"
|
||||
"SDL_GetCurrentVideoDriver = %s\n"
|
||||
"SDL_GetDisplayName = %s\n"
|
||||
"SDL_GetCPUCount = %d\n"
|
||||
"SDL_GetCPUCacheLineSize = %d\n"
|
||||
/*"s.format.palette.ncolors = %d\n"*/,
|
||||
(*s).w, (*s).h, (*s).flags, (*s).pitch, (*s).pixels,
|
||||
(*s).locked, (*(*s).format).format,
|
||||
(*(*s).format).BitsPerPixel,
|
||||
(*(*s).format).BytesPerPixel,
|
||||
SDL_GetNumVideoDrivers(),
|
||||
SDL_GetCurrentVideoDriver(),
|
||||
SDL_GetDisplayName(0),
|
||||
SDL_GetCPUCount(),
|
||||
SDL_GetCPUCacheLineSize()
|
||||
/*(*(*(*s).format).palette).ncolors*/
|
||||
);
|
||||
|
||||
/*
|
||||
SDL_Renderer * r = SDL_CreateRenderer(
|
||||
w, -1,
|
||||
0
|
||||
| SDL_RENDERER_ACCELERATED
|
||||
| SDL_RENDERER_PRESENTVSYNC
|
||||
| SDL_RENDERER_TARGETTEXTURE
|
||||
);
|
||||
*/
|
||||
|
||||
enum {
|
||||
RNDWLK,
|
||||
RNDLNS
|
||||
};
|
||||
|
||||
SDL_Event e;
|
||||
uint16_t i = 0xffff;
|
||||
uint8_t _r, _g, _b, m=RNDWLK;
|
||||
SDL_Point * points = malloc(sizeof(SDL_Point)*i);
|
||||
SDL_SetRenderDrawBlendMode(r, SDL_BLENDMODE_NONE);
|
||||
|
||||
uint16_t x=RND()%std.w;
|
||||
uint16_t y=RND()%std.h;
|
||||
|
||||
do{
|
||||
/* Get keyboard and mouse events */
|
||||
SDL_PollEvent(&e);
|
||||
|
||||
/* q quits */
|
||||
if((e.key.keysym.sym == SDLK_q)
|
||||
/*
|
||||
& (SDL_GetModState() == KMOD_LCTRL)
|
||||
*/
|
||||
) break;
|
||||
|
||||
if(e.key.keysym.sym == SDLK_m)
|
||||
SDL_SetRenderDrawBlendMode(r, SDL_BLENDMODE_MOD);
|
||||
if(e.key.keysym.sym == SDLK_n)
|
||||
SDL_SetRenderDrawBlendMode(r, SDL_BLENDMODE_NONE);
|
||||
if(e.key.keysym.sym == SDLK_b)
|
||||
SDL_SetRenderDrawBlendMode(r, SDL_BLENDMODE_BLEND);
|
||||
if(e.key.keysym.sym == SDLK_a)
|
||||
SDL_SetRenderDrawBlendMode(r, SDL_BLENDMODE_ADD);
|
||||
if(e.key.keysym.sym == SDLK_l)
|
||||
m = RNDLNS;
|
||||
if(e.key.keysym.sym == SDLK_w)
|
||||
m = RNDWLK;
|
||||
if(e.key.keysym.sym == SDLK_c){
|
||||
SDL_SetRenderDrawColor(r, 0, 0, 0, 0xff);
|
||||
SDL_RenderClear(r);
|
||||
}
|
||||
|
||||
/* Random Colors */
|
||||
_r += RND()%2 ? _r-1 ? 1 : 1 : -1;
|
||||
_g += RND()%2 ? _g-1 ? 1 : 1 : -1;
|
||||
_b += RND()%2 ? _b-1 ? 1 : 1 : -1;
|
||||
|
||||
if(m == RNDLNS) goto rndlns;
|
||||
if(m == RNDWLK) goto rndwlk;
|
||||
|
||||
/* clear the canvas
|
||||
SDL_SetRenderDrawColor(r, 0x0f, 0x0f, 0x0f, 0x01);
|
||||
SDL_RenderClear(r);
|
||||
*/
|
||||
|
||||
/* Set actual canvas to be the renderer r */
|
||||
SDL_SetRenderTarget(r, 0x00);
|
||||
|
||||
/* Random Lines
|
||||
*/
|
||||
rndlns:
|
||||
i = 0x7ff;
|
||||
while(--i)
|
||||
points[i] =
|
||||
(struct SDL_Point) { RND()%std.w, RND()%std.h};
|
||||
|
||||
SDL_SetRenderDrawColor(r, _r, _g, _b, 0x0f);
|
||||
i = 0x7ff;
|
||||
SDL_RenderDrawLines(r, points, i);
|
||||
goto show;
|
||||
|
||||
/* Random Walker */
|
||||
i = 0xffff;
|
||||
rndwlk:
|
||||
x += RND()%2 ? x+1 < std.w ? 1 : 0 : x-1 < 0 ? 0 : -1;
|
||||
y += RND()%2 ? y+1 < std.h ? 1 : 0 : y-1 < 0 ? 0 : -1;
|
||||
|
||||
SDL_SetRenderDrawColor(r, _r, _g, _b, 0x0f);
|
||||
SDL_RenderDrawPoint(r, x, y);
|
||||
i--;
|
||||
if(i) goto rndwlk;
|
||||
goto show;
|
||||
|
||||
show:
|
||||
/* Show the canvas */
|
||||
SDL_RenderPresent(r);
|
||||
|
||||
/* Updates the screen? */
|
||||
SDL_UpdateWindowSurface(w);
|
||||
/*
|
||||
break;
|
||||
*/
|
||||
SDL_Delay(40);
|
||||
}while(1);
|
||||
|
||||
SDL_DestroyWindow(w);
|
||||
/*SDL_FreeSurface(s);*/
|
||||
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue