initial commit
This commit is contained in:
3 files changed
+200
No files matched your search
@@ -0,0 +1,2 @@
|
|||||||
|
main.o
|
||||||
|
app
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
CC = gcc
|
||||||
|
CFLAGS = -Wall -Wextra -std=c17 `sdl2-config --cflags`
|
||||||
|
LDFLAGS = `sdl2-config --libs`
|
||||||
|
|
||||||
|
SRC = $(wildcard *.c)
|
||||||
|
OBJ = $(SRC:.c=.o)
|
||||||
|
TARGET = app
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
$(TARGET): $(OBJ)
|
||||||
|
$(CC) $(OBJ) -o $(TARGET) $(LDFLAGS)
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
run: $(TARGET)
|
||||||
|
./$(TARGET)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(OBJ) $(TARGET)
|
||||||
@@ -0,0 +1,177 @@
|
|||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <SDL2/SDL_render.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define WINDOW_W 800
|
||||||
|
#define WINDOW_H 600
|
||||||
|
|
||||||
|
#define NUM_RAINDROPS 300
|
||||||
|
#define MAX_WIND 30
|
||||||
|
#define GRAVITY 400
|
||||||
|
#define GRAVITY_DIFF 50 // idk, maybe some downward or upward wind causing resistance
|
||||||
|
#define RAINDROP_WIDTH 2
|
||||||
|
#define RAINDROP_MAX_HEIGHT 30
|
||||||
|
#define RAINDROP_MIN_HEIGHT 10
|
||||||
|
|
||||||
|
#define BACKGROUND_R 20
|
||||||
|
#define BACKGROUND_G 20
|
||||||
|
#define BACKGROUND_B 20
|
||||||
|
#define BACKGROUND_A 255
|
||||||
|
#define RAINDROP_R 156
|
||||||
|
#define RAINDROP_G 174
|
||||||
|
#define RAINDROP_B 255
|
||||||
|
#define RAINDROP_A 255
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float accelerationX;
|
||||||
|
float accelerationY;
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
} RAINDROP;
|
||||||
|
|
||||||
|
float rand_float(float min, float max)
|
||||||
|
{
|
||||||
|
float scale = (float)rand() / (float)RAND_MAX;
|
||||||
|
return min + scale * (max - min);
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset_raindrop(RAINDROP *d)
|
||||||
|
{
|
||||||
|
d->x = rand_float(0, WINDOW_W);
|
||||||
|
d->y = rand_float(-1 * WINDOW_H, 0);
|
||||||
|
d->accelerationX = rand_float(-1 * MAX_WIND, MAX_WIND);
|
||||||
|
d->accelerationY = rand_float(GRAVITY - GRAVITY_DIFF, GRAVITY + GRAVITY_DIFF);
|
||||||
|
d->width = RAINDROP_WIDTH;
|
||||||
|
d->height = rand() % RAINDROP_MAX_HEIGHT + RAINDROP_MIN_HEIGHT;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
if (SDL_Init(SDL_INIT_VIDEO) != 0)
|
||||||
|
{
|
||||||
|
SDL_Log("Error initializing: %s", SDL_GetError());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Window *window;
|
||||||
|
SDL_Renderer *renderer;
|
||||||
|
|
||||||
|
window = SDL_CreateWindow(
|
||||||
|
"SDL2 Window",
|
||||||
|
SDL_WINDOWPOS_CENTERED,
|
||||||
|
SDL_WINDOWPOS_CENTERED,
|
||||||
|
WINDOW_W,
|
||||||
|
WINDOW_H,
|
||||||
|
SDL_WINDOW_SHOWN
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!window)
|
||||||
|
{
|
||||||
|
SDL_Log("Error creating window: %s", SDL_GetError());
|
||||||
|
SDL_Quit();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
renderer = SDL_CreateRenderer(
|
||||||
|
window,
|
||||||
|
-1,
|
||||||
|
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!renderer)
|
||||||
|
{
|
||||||
|
SDL_Log("Error creating renderer: %s", SDL_GetError());
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
SDL_Quit();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int running;
|
||||||
|
SDL_Event event;
|
||||||
|
Uint32 last_time;
|
||||||
|
|
||||||
|
last_time = SDL_GetTicks();
|
||||||
|
running = 1;
|
||||||
|
|
||||||
|
// Create drops
|
||||||
|
RAINDROP drops[NUM_RAINDROPS];
|
||||||
|
for (int i = 0; i < NUM_RAINDROPS; i++)
|
||||||
|
{
|
||||||
|
reset_raindrop(&drops[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (running)
|
||||||
|
{
|
||||||
|
while (SDL_PollEvent(&event))
|
||||||
|
{
|
||||||
|
if (event.type == SDL_QUIT)
|
||||||
|
{
|
||||||
|
running = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// delta time
|
||||||
|
Uint32 now;
|
||||||
|
float delta;
|
||||||
|
|
||||||
|
now = SDL_GetTicks();
|
||||||
|
delta = (now - last_time) / 1000.0f; // seconds
|
||||||
|
last_time = now;
|
||||||
|
|
||||||
|
// clear scren
|
||||||
|
SDL_SetRenderDrawColor(
|
||||||
|
renderer,
|
||||||
|
BACKGROUND_R,
|
||||||
|
BACKGROUND_G,
|
||||||
|
BACKGROUND_B,
|
||||||
|
BACKGROUND_A
|
||||||
|
);
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
|
||||||
|
|
||||||
|
// render raindrops
|
||||||
|
for (int i = 0; i < NUM_RAINDROPS; i++)
|
||||||
|
{
|
||||||
|
RAINDROP *drop;
|
||||||
|
drop = &drops[i];
|
||||||
|
|
||||||
|
drop->x += delta * drop->accelerationX;
|
||||||
|
drop->y += delta * drop->accelerationY;
|
||||||
|
|
||||||
|
if (drop->y > WINDOW_H ||
|
||||||
|
drop->x < -1 * RAINDROP_WIDTH ||
|
||||||
|
drop->x > WINDOW_W + RAINDROP_WIDTH)
|
||||||
|
{
|
||||||
|
reset_raindrop(drop);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Rect raindrop;
|
||||||
|
|
||||||
|
raindrop = (SDL_Rect){
|
||||||
|
(int) drop->x,
|
||||||
|
(int) drop->y,
|
||||||
|
drop->width,
|
||||||
|
drop->height
|
||||||
|
};
|
||||||
|
|
||||||
|
// Set brush color and draw
|
||||||
|
SDL_SetRenderDrawColor(
|
||||||
|
renderer,
|
||||||
|
RAINDROP_R,
|
||||||
|
RAINDROP_G,
|
||||||
|
RAINDROP_B,
|
||||||
|
RAINDROP_A
|
||||||
|
);
|
||||||
|
SDL_RenderFillRect(renderer, &raindrop);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_DestroyRenderer(renderer);
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
SDL_Quit();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user