Browse Source

Ready. Set. Go!

rexim 4 years ago
commit
8f2b65df5e
5 changed files with 120 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 20 0
      LICENSE
  3. 5 0
      Makefile
  4. 14 0
      README.md
  5. 80 0
      main.c

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+bezier

+ 20 - 0
LICENSE

@@ -0,0 +1,20 @@
+Copyright 2021 Alexey Kutepov <[email protected]>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+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 OR COPYRIGHT HOLDERS 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.

+ 5 - 0
Makefile

@@ -0,0 +1,5 @@
+CFLAGS=-Wall -Wextra -std=c11 -pedantic `pkg-config --cflags sdl2`
+LIBS=`pkg-config --libs sdl2`
+
+bezier: main.c
+	$(CC) $(CFLAGS) -o bezier main.c $(LIBS)

+ 14 - 0
README.md

@@ -0,0 +1,14 @@
+# Bézier Curve
+
+Just a fun little project to learn how to render Bézier Curves.
+
+## Quick Start
+
+```console
+$ make
+$ ./bezier
+```
+
+## References
+
+- https://www.geogebra.org/m/WPHQ9rUt

+ 80 - 0
main.c

@@ -0,0 +1,80 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <SDL.h>
+
+#define SCREEN_WIDTH 800
+#define SCREEN_HEIGHT 600
+#define BACKGROUND_COLOR 0x000000FF
+
+#define HEX_COLOR(hex)                      \
+    ((hex) >> (3 * 8)) & 0xFF,              \
+    ((hex) >> (2 * 8)) & 0xFF,              \
+    ((hex) >> (1 * 8)) & 0xFF,              \
+    ((hex) >> (0 * 8)) & 0xFF
+
+int check_sdl_code(int code)
+{
+    if (code < 0) {
+        fprintf(stderr, "SDL error: %s\n", SDL_GetError());
+        exit(1);
+    }
+
+    return code;
+}
+
+void *check_sdl_ptr(void *ptr)
+{
+    if (ptr == NULL) {
+        fprintf(stderr, "SDL error: %s\n", SDL_GetError());
+        exit(1);
+    }
+
+    return ptr;
+}
+
+int main(void)
+{
+    check_sdl_code(
+        SDL_Init(SDL_INIT_VIDEO));
+
+    SDL_Window * const window =
+        check_sdl_ptr(
+            SDL_CreateWindow(
+                "Bezier Curves",
+                0, 0,
+                SCREEN_WIDTH,
+                SCREEN_HEIGHT,
+                SDL_WINDOW_RESIZABLE));
+
+    SDL_Renderer * const renderer =
+        check_sdl_ptr(
+            SDL_CreateRenderer(
+                window, -1, SDL_RENDERER_ACCELERATED));
+
+    int quit = 0;
+    while (!quit) {
+        SDL_Event event;
+        while (SDL_PollEvent(&event)) {
+            switch (event.type) {
+            case SDL_QUIT:
+                quit = 1;
+                break;
+            }
+        }
+
+        check_sdl_code(
+            SDL_SetRenderDrawColor(
+                renderer,
+                HEX_COLOR(BACKGROUND_COLOR)));
+
+        check_sdl_code(
+            SDL_RenderClear(renderer));
+
+        SDL_RenderPresent(renderer);
+    }
+
+    SDL_Quit();
+
+    return 0;
+}