Commit 5b589ec3ec22303870595e96af6f506cae7c39cd
0 parents
Initial version
Showing
6 changed files
with
235 additions
and
0 deletions
hanoi/makefile
0 → 100755
| 1 | +++ a/hanoi/makefile | |
| 1 | +TARGET=sun | |
| 2 | + | |
| 3 | +all: $(TARGET) | |
| 4 | + | |
| 5 | +%.o:%.c | |
| 6 | + gcc -g -I.. -Wall -pedantic `sdl2-config --cflags` -c $< -o $@ | |
| 7 | + | |
| 8 | +%: %.c ../primlib.o %.c | |
| 9 | + gcc -g -I.. -Wall -pedantic `sdl2-config --cflags` ../primlib.o $< -o $@ -lSDL2_gfx `sdl2-config --libs` | |
| 10 | + | |
| 11 | +../primlib.o: ../primlib.c ../primlib.h | |
| 12 | + | |
| 13 | +$(TARGET): $(TARGET).c ../primlib.h | |
| 14 | + | |
| 15 | +clean: | |
| 16 | + -rm $(TARGET) | |
| 0 | 17 | \ No newline at end of file | ... | ... |
hanoi/sun.c
0 → 100644
| 1 | +++ a/hanoi/sun.c | |
| 1 | +#include "primlib.h" | |
| 2 | +#include <stdlib.h> | |
| 3 | +#include <unistd.h> | |
| 4 | + | |
| 5 | +int main(int argc, char *argv[]) { | |
| 6 | + if (gfx_init()) | |
| 7 | + exit(3); | |
| 8 | + /* clear screen */ | |
| 9 | + gfx_filledRect(0, 0, gfx_screenWidth() - 1, gfx_screenHeight() - 1, BLUE); | |
| 10 | + gfx_filledCircle(gfx_screenWidth() / 2, gfx_screenHeight() / 4, 100, YELLOW); | |
| 11 | + gfx_updateScreen(); | |
| 12 | + gfx_getkey(); | |
| 13 | + return 0; | |
| 14 | +} | ... | ... |
makefile
0 → 100644
| 1 | +++ a/makefile | |
| 1 | +all: testlib | |
| 2 | + | |
| 3 | +testlib: testlib.o primlib.o | |
| 4 | + gcc -g $^ -o $@ -lSDL2_gfx `sdl2-config --libs` | |
| 5 | + | |
| 6 | +.c.o: | |
| 7 | + gcc -g -Wall -pedantic `sdl2-config --cflags` -c $< | |
| 8 | + | |
| 9 | +primlib.o: primlib.c primlib.h | |
| 10 | + | |
| 11 | +testlib.o: testlib.c primlib.h | |
| 12 | + | |
| 13 | +clean: | |
| 14 | + -rm primlib.o testlib.o testlib | |
| 0 | 15 | \ No newline at end of file | ... | ... |
primlib.c
0 → 100644
| 1 | +++ a/primlib.c | |
| 1 | +#include <assert.h> | |
| 2 | +#include <math.h> | |
| 3 | +#include <stdio.h> | |
| 4 | +#include <stdlib.h> | |
| 5 | +#include <string.h> | |
| 6 | +#include <time.h> | |
| 7 | + | |
| 8 | +#include "primlib.h" | |
| 9 | + | |
| 10 | +static SDL_Renderer *renderer = NULL; | |
| 11 | +static SDL_Window *window = NULL; | |
| 12 | + | |
| 13 | +// static Uint32 colors[COLOR_MAX] = {0xff000000, | |
| 14 | +// 0xff0000ff, 0xff00ff00, 0xffff0000, | |
| 15 | +// 0xffffff00, 0xffff00ff, 0xff00ffff, | |
| 16 | +// 0xffffffff}; | |
| 17 | + | |
| 18 | +struct RGB { | |
| 19 | + uint8_t r; | |
| 20 | + uint8_t g; | |
| 21 | + uint8_t b; | |
| 22 | +}; | |
| 23 | + | |
| 24 | +static struct RGB colors[COLOR_MAX] = { | |
| 25 | + {0, 0, 0}, {255, 0, 0}, {0, 255, 0}, {0, 0, 255}, | |
| 26 | + {0, 255, 255}, {255, 0, 255}, {255, 255, 0}, {255, 255, 255}}; | |
| 27 | + | |
| 28 | +// Screen dimension constants | |
| 29 | +const int SCREEN_WIDTH = 640; | |
| 30 | +const int SCREEN_HEIGHT = 480; | |
| 31 | + | |
| 32 | +void gfx_pixel(int x, int y, enum color c) { | |
| 33 | + pixelRGBA(renderer, x, y, colors[c].r, colors[c].g, colors[c].b, 255); | |
| 34 | +} | |
| 35 | + | |
| 36 | +void gfx_line(int x1, int y1, int x2, int y2, enum color c) { | |
| 37 | + assert(c < COLOR_MAX); | |
| 38 | + lineRGBA(renderer, x1, y1, x2, y2, colors[c].r, colors[c].g, colors[c].b, | |
| 39 | + 255); | |
| 40 | +} | |
| 41 | + | |
| 42 | +void gfx_rect(int x1, int y1, int x2, int y2, enum color c) { | |
| 43 | + assert(c < COLOR_MAX); | |
| 44 | + rectangleRGBA(renderer, x1, y1, x2, y2, colors[c].r, colors[c].g, colors[c].b, | |
| 45 | + 255); | |
| 46 | +} | |
| 47 | + | |
| 48 | +void gfx_filledRect(int x1, int y1, int x2, int y2, enum color c) { | |
| 49 | + assert(c < COLOR_MAX); | |
| 50 | + boxRGBA(renderer, x1, y1, x2, y2, colors[c].r, colors[c].g, colors[c].b, 255); | |
| 51 | +} | |
| 52 | + | |
| 53 | +void gfx_circle(int x, int y, int r, enum color c) { | |
| 54 | + assert(c < COLOR_MAX); | |
| 55 | + circleRGBA(renderer, x, y, r, colors[c].r, colors[c].g, colors[c].b, 255); | |
| 56 | +} | |
| 57 | + | |
| 58 | +void gfx_filledCircle(int x, int y, int r, enum color c) { | |
| 59 | + assert(c < COLOR_MAX); | |
| 60 | + filledCircleRGBA(renderer, x, y, r, colors[c].r, colors[c].g, colors[c].b, | |
| 61 | + 255); | |
| 62 | +} | |
| 63 | + | |
| 64 | +int gfx_screenWidth() { return SCREEN_WIDTH; } | |
| 65 | + | |
| 66 | +int gfx_screenHeight() { return SCREEN_HEIGHT; } | |
| 67 | + | |
| 68 | +void gfx_updateScreen() { | |
| 69 | + SDL_RenderPresent(renderer); | |
| 70 | + SDL_RenderClear(renderer); | |
| 71 | +} | |
| 72 | + | |
| 73 | +void gfx_textout(int x, int y, const char *s, enum color c) { | |
| 74 | + assert(c < COLOR_MAX); | |
| 75 | + stringRGBA(renderer, x, y, s, colors[c].r, colors[c].g, colors[c].b, 255); | |
| 76 | +} | |
| 77 | + | |
| 78 | +int gfx_pollkey() { | |
| 79 | + SDL_Event event; | |
| 80 | + while (SDL_PollEvent(&event)) { | |
| 81 | + switch (event.type) { | |
| 82 | + case SDL_KEYDOWN: | |
| 83 | + return event.key.keysym.sym; | |
| 84 | + case SDL_QUIT: | |
| 85 | + exit(3); | |
| 86 | + } | |
| 87 | + } | |
| 88 | + return -1; | |
| 89 | +} | |
| 90 | + | |
| 91 | +int gfx_getkey() { | |
| 92 | + SDL_Event event; | |
| 93 | + while (1) { | |
| 94 | + SDL_WaitEvent(&event); | |
| 95 | + if (event.type == SDL_KEYDOWN) | |
| 96 | + break; | |
| 97 | + if (event.type == SDL_QUIT) | |
| 98 | + exit(3); | |
| 99 | + }; | |
| 100 | + return event.key.keysym.sym; | |
| 101 | +} | |
| 102 | + | |
| 103 | +int gfx_isKeyDown(int key) { | |
| 104 | + const Uint8 *keytable; | |
| 105 | + int numkeys; | |
| 106 | + SDL_PumpEvents(); | |
| 107 | + keytable = SDL_GetKeyboardState(&numkeys); | |
| 108 | + SDL_Scancode code = SDL_GetScancodeFromKey(key); | |
| 109 | + assert(code < numkeys); | |
| 110 | + return keytable[code]; | |
| 111 | +} | |
| 112 | + | |
| 113 | +static void gfx_close() { | |
| 114 | + SDL_DestroyRenderer(renderer); | |
| 115 | + SDL_DestroyWindow(window); | |
| 116 | + SDL_Quit(); | |
| 117 | +} | |
| 118 | + | |
| 119 | +int gfx_init() { | |
| 120 | + | |
| 121 | + /* Initialize SDL */ | |
| 122 | + if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
| 123 | + fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); | |
| 124 | + return 1; | |
| 125 | + } | |
| 126 | + atexit(gfx_close); | |
| 127 | + | |
| 128 | + window = SDL_CreateWindow("SDL", SDL_WINDOWPOS_UNDEFINED, | |
| 129 | + SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, | |
| 130 | + SCREEN_HEIGHT, SDL_WINDOW_SHOWN); | |
| 131 | + if (window == NULL) { | |
| 132 | + printf("Window could not be created! SDL Error: %s\n", SDL_GetError()); | |
| 133 | + exit(3); | |
| 134 | + } else { | |
| 135 | + // Create renderer for window | |
| 136 | + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE); | |
| 137 | + if (renderer == NULL) { | |
| 138 | + printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError()); | |
| 139 | + exit(3); | |
| 140 | + } | |
| 141 | + } | |
| 142 | + | |
| 143 | + SDL_Delay(10); | |
| 144 | + return 0; | |
| 145 | +} | ... | ... |
primlib.h
0 → 100644
| 1 | +++ a/primlib.h | |
| 1 | +#ifndef __PRIMLIB_H__ | |
| 2 | +#define __PRIMLIB_H__ | |
| 3 | + | |
| 4 | +#include <SDL2/SDL.h> | |
| 5 | +#include <SDL2/SDL2_gfxPrimitives.h> | |
| 6 | +int gfx_init(); | |
| 7 | +enum color { BLACK, RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, WHITE, COLOR_MAX }; | |
| 8 | +void gfx_pixel(int x, int y, enum color c); | |
| 9 | +void gfx_line(int x1, int y1, int x2, int y2, enum color c); | |
| 10 | +void gfx_circle(int x, int y, int r, enum color c); | |
| 11 | +void gfx_filledRect(int x1, int y1, int x2, int y2, enum color c); | |
| 12 | +void gfx_filledCircle(int x, int y, int r, enum color c); | |
| 13 | +void gfx_rect(int x1, int y1, int x2, int y2, enum color c); | |
| 14 | +void gfx_textout(int x, int y, const char *s, enum color c); | |
| 15 | +int gfx_screenWidth(); | |
| 16 | +int gfx_screenHeight(); | |
| 17 | +void gfx_updateScreen(); | |
| 18 | +int gfx_pollkey(); | |
| 19 | +int gfx_getkey(); | |
| 20 | +int gfx_isKeyDown(int key); | |
| 21 | + | |
| 22 | +#endif /* __PRIMLIB_H__ */ | ... | ... |
testlib.c
0 → 100644
| 1 | +++ a/testlib.c | |
| 1 | +#include "primlib.h" | |
| 2 | +#include <stdlib.h> | |
| 3 | +#include <unistd.h> | |
| 4 | + | |
| 5 | +int main(int argc, char *argv[]) { | |
| 6 | + if (gfx_init()) | |
| 7 | + exit(3); | |
| 8 | + /* clear screen */ | |
| 9 | + for (int i = -99; i < 100; ++i) { | |
| 10 | + gfx_filledRect(0, 0, gfx_screenWidth() - 1, gfx_screenHeight() - 1, BLACK); | |
| 11 | + gfx_filledCircle(gfx_screenWidth() / 2 + i, gfx_screenHeight() / 2, 100, YELLOW); | |
| 12 | + gfx_textout(gfx_screenWidth() / 2 - i, gfx_screenHeight() / 2, "This is a text", RED); | |
| 13 | + gfx_updateScreen(); | |
| 14 | + SDL_Delay(10); | |
| 15 | + } | |
| 16 | + gfx_getkey(); | |
| 17 | + return 0; | |
| 18 | +} | ... | ... |