浏览代码

new: bouncing ball using SDL

Douglas A 3 年之前
父节点
当前提交
d1c480fc1a
共有 3 个文件被更改,包括 112 次插入0 次删除
  1. 10 0
      bouncing-ball/Makefile
  2. 二进制
      bouncing-ball/bouncing
  3. 102 0
      bouncing-ball/bouncing.c

+ 10 - 0
bouncing-ball/Makefile

@@ -0,0 +1,10 @@
+PROGRAM = bouncing
+CFLAGS = -Wall -g -O0
+
+all: $(PROGRAM)
+
+$(PROGRAM): $(PROGRAM).o
+	$(CC) $(CFLAGS) `pkg-config --cflags --libs sdl2` -o $@ $<
+
+$(PROGRAM).o: $(PROGRAM).c
+	$(CC) $(CFLAGS) -c `pkg-config --cflags sdl2` -o $@ $<

二进制
bouncing-ball/bouncing


+ 102 - 0
bouncing-ball/bouncing.c

@@ -0,0 +1,102 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+#include <SDL2/SDL.h>
+#define WIDTH 800
+#define HEIGHT 600
+
+typedef struct {
+        int x;
+        int y;
+} vec2d;
+
+typedef struct {
+        vec2d pos;
+        vec2d speed;
+        vec2d acc;
+        int size;
+} ball;
+
+vec2d add_vec2d(vec2d a, vec2d b){
+        vec2d c = { .x = 0, .y = 0 };
+        c.x = a.x + b.x;
+        c.y = a.y + b.y;
+
+        return c;
+}
+
+void bounce_ball(ball *b, int x, int y) {
+        if (b->pos.x >= x || b->pos.x < 0) 
+                b->speed.x *= -1;
+        if (b->pos.y >= y || b->pos.y < 0)
+                b->speed.y *= -1;
+}
+
+
+void update_ball(ball *b) {
+        b->pos = add_vec2d(b->pos, b->speed);
+        b->speed = add_vec2d(b->speed, b->acc);
+        bounce_ball(b, WIDTH, HEIGHT);
+}
+
+void update(SDL_Renderer *ren, unsigned int ticks, ball *b){
+        SDL_SetRenderDrawColor(ren, 128, 128, 128, 255);
+        SDL_RenderClear(ren);
+
+
+        SDL_SetRenderDrawColor(ren, 255, 0, 0, 255);
+        SDL_Rect ballrect = {
+                .x = b->pos.x,
+                .y = b->pos.y,
+                .w = b->size / 2,
+                .h = b->size / 2
+        };
+
+        SDL_RenderFillRect(ren, &ballrect);
+        update_ball(b);
+
+}
+
+
+int main(int argc, char** argv) {
+        if (SDL_Init(SDL_INIT_VIDEO) != 0) {
+                fprintf(stderr, "Error initializing SDL");
+                return 1;
+        }
+        ball b;
+        b.pos.x = 100;
+        b.pos.y = 100;
+        b.speed.x = 5;
+        b.speed.y = 1;
+        b.acc.x = 0;
+        b.acc.y = 0;
+        b.size = 30;
+
+        SDL_Window *win = SDL_CreateWindow("New Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
+        SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
+        bool running = true;
+        SDL_Event ev;
+        unsigned int ticks = 0;
+        while (running) {
+                while (SDL_PollEvent(&ev)){
+                        ticks = SDL_GetTicks();
+                        if (ev.type == SDL_QUIT){
+                                running = false;
+                                break;
+                        }
+                        unsigned int newticks = SDL_GetTicks();
+                        ticks = (newticks - ticks) / 1000;
+                }
+
+                update(ren, ticks, &b);
+                SDL_RenderPresent(ren);
+
+        }
+        SDL_DestroyRenderer(ren);
+        SDL_DestroyWindow(win);
+        SDL_Quit();
+
+        return (0);
+}
+