From d82f01d11fd3e8d9a19b6c0c61f663636d159ce5 Mon Sep 17 00:00:00 2001
From: Orestis Malaspinas <orestis.malaspinas@hesge.ch>
Date: Mon, 13 May 2019 13:16:11 +0200
Subject: [PATCH] added gitignore

---
 lib/.gitignore |   3 ++
 lib/Makefile   |  16 +++++++
 lib/gfx.c      | 128 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/gfx.h      |  41 ++++++++++++++++
 lib/lbm.c      |  55 +++++++++++++++++++++
 lib/point.h    |   9 ++++
 6 files changed, 252 insertions(+)
 create mode 100644 lib/.gitignore
 create mode 100644 lib/Makefile
 create mode 100644 lib/gfx.c
 create mode 100644 lib/gfx.h
 create mode 100644 lib/lbm.c
 create mode 100644 lib/point.h

diff --git a/lib/.gitignore b/lib/.gitignore
new file mode 100644
index 0000000..da1861b
--- /dev/null
+++ b/lib/.gitignore
@@ -0,0 +1,3 @@
+*.o
+*.gch
+lbm
diff --git a/lib/Makefile b/lib/Makefile
new file mode 100644
index 0000000..27b77fe
--- /dev/null
+++ b/lib/Makefile
@@ -0,0 +1,16 @@
+CC = gcc
+FLAGS = -g -Wall -Wextra -std=gnu11
+SANITIZERS = -fsanitize=address -fsanitize=leak -fsanitize=undefined
+LIBS = -lSDL2
+
+lbm: lbm.o gfx.o
+	$(CC) $(FLAGS) $(OPT) $^ -o $@ $(LIBS)  $(SANITIZERS)
+
+lbm.o: lbm.c gfx.h
+	$(CC) $(FLAGS) $(OPT) $^ -c
+
+gfx.o: gfx.c gfx.h
+	$(CC) $(FLAGS) $(OPT) $< -c
+
+clean:
+	rm -f lbm *.o
\ No newline at end of file
diff --git a/lib/gfx.c b/lib/gfx.c
new file mode 100644
index 0000000..2a270e0
--- /dev/null
+++ b/lib/gfx.c
@@ -0,0 +1,128 @@
+/// @file gfx.c
+/// @author Florent Gluck
+/// @date November 6, 2016
+/// Helper routines to render pixels in fullscreen graphic mode.
+/// Uses the SDL2 library.
+
+#include "gfx.h"
+
+/// Create a fullscreen graphic window.
+/// @param title Title of the window.
+/// @param width Width of the window in pixels.
+/// @param height Height of the window in pixels.
+/// @return a pointer to the graphic context or NULL if it failed.
+struct gfx_context_t* gfx_create(char *title, uint width, uint height) {
+	if (SDL_Init(SDL_INIT_VIDEO) != 0) goto error;
+	SDL_Window *window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
+			SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_RESIZABLE);
+	SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
+	SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
+			SDL_TEXTUREACCESS_STREAMING, width, height);
+	uint32_t *pixels = malloc(width*height*sizeof(uint32_t));
+	struct gfx_context_t *ctxt = malloc(sizeof(struct gfx_context_t));
+
+	if (!window || !renderer || !texture || !pixels || !ctxt) goto error;
+
+	ctxt->renderer = renderer;
+	ctxt->texture = texture;
+	ctxt->window = window;
+	ctxt->width = width;
+	ctxt->height = height;
+	ctxt->pixels = pixels;
+
+	SDL_ShowCursor(SDL_DISABLE);
+	gfx_clear(ctxt, COLOR_BLACK);
+	return ctxt;
+
+error:
+	return NULL;
+}
+
+/// Draw a pixel in the specified graphic context.
+/// @param ctxt Graphic context where the pixel is to be drawn.
+/// @param x X coordinate of the pixel.
+/// @param y Y coordinate of the pixel.
+/// @param color Color of the pixel.
+void gfx_putpixel(struct gfx_context_t *ctxt, int x, int y, uint32_t color) {
+	if (x < ctxt->width && y < ctxt->height)
+		ctxt->pixels[ctxt->width*y+x] = color;
+}
+
+/// Clear the specified graphic context.
+/// @param ctxt Graphic context to clear.
+/// @param color Color to use.
+void gfx_clear(struct gfx_context_t *ctxt, uint32_t color) {
+	int n = ctxt->width*ctxt->height;
+	while (n)
+		ctxt->pixels[--n] = color;
+}
+
+/// Display the graphic context.
+/// @param ctxt Graphic context to clear.
+void gfx_present(struct gfx_context_t *ctxt) {
+	SDL_UpdateTexture(ctxt->texture, NULL, ctxt->pixels, ctxt->width*sizeof(uint32_t));
+	SDL_RenderCopy(ctxt->renderer, ctxt->texture, NULL, NULL);
+	SDL_RenderPresent(ctxt->renderer);
+}
+
+/// Destroy a graphic window.
+/// @param ctxt Graphic context of the window to close.
+void gfx_destroy(struct gfx_context_t *ctxt) {
+	SDL_ShowCursor(SDL_ENABLE);
+	SDL_DestroyTexture(ctxt->texture);
+	SDL_DestroyRenderer(ctxt->renderer);
+	SDL_DestroyWindow(ctxt->window);
+	free(ctxt->pixels);
+	ctxt->texture = NULL;
+	ctxt->renderer = NULL;
+	ctxt->window = NULL;
+	ctxt->pixels = NULL;
+	SDL_Quit();
+	free(ctxt);
+}
+
+/// If a key was pressed, returns its key code (non blocking call).
+/// List of key codes: https://wiki.libsdl.org/SDL_Keycode
+/// SDL_PumpEvents() must be called before.
+/// @return 0 if escape was not pressed.
+SDL_Keycode gfx_keypressed() {
+	const Uint8 *state = SDL_GetKeyboardState(NULL);
+	if (state && state[SDL_SCANCODE_ESCAPE]) {
+		return SDLK_ESCAPE;
+	}
+	return 0;
+}
+
+/// If the mouse left button was pressed, p contains its position
+/// relative to window (non blocking call).
+/// SDL_PumpEvents() must be called before.
+/// @return 0 if no button was pressed and modifies the location
+/// otherwise.
+int gfx_left_mouse_pressed(point *p) {
+	return (SDL_GetMouseState(&p->x, &p->y) & SDL_BUTTON(SDL_BUTTON_LEFT));
+}
+
+/// If the mouse right button was pressed, p contains its position
+/// relative to window (non blocking call).
+/// SDL_PumpEvents() must be called before.
+/// @return 0 if no button was pressed and modifies the location
+/// otherwise.
+int gfx_right_mouse_pressed(point *p) {
+	return (SDL_GetMouseState(&p->x, &p->y) & SDL_BUTTON(SDL_BUTTON_RIGHT));
+}
+
+
+int gfx_left_right_mouse_pressed(point *p) {
+    Uint32 ret = SDL_GetMouseState(&p->x, &p->y) & (SDL_BUTTON(SDL_BUTTON_LEFT) | SDL_BUTTON(SDL_BUTTON_RIGHT));
+
+	if (ret == 1) {
+        // the right button was pressed
+		printf("Left clic.\n");
+		return ret;
+    } else if (ret == 4) {
+        // the left button was pressed
+		printf("Right clic.\n");
+		return ret;
+    } 
+    return 0; // neither was pressed
+}
diff --git a/lib/gfx.h b/lib/gfx.h
new file mode 100644
index 0000000..b4a1f44
--- /dev/null
+++ b/lib/gfx.h
@@ -0,0 +1,41 @@
+#ifndef _GFX_H_
+#define _GFX_H_
+
+#include <stdint.h>
+#include <SDL2/SDL.h>
+#include "point.h"
+
+#define MAKE_COLOR(r,g,b) ((uint32_t)b|((uint32_t)g<<8)|((uint32_t)r<<16))
+
+#define COLOR_BLACK  0x00000000
+#define COLOR_RED    0x00FF0000
+#define COLOR_GREEN  0x0000FF00
+#define COLOR_BLUE   0x000000FF
+#define COLOR_WHITE  0x00FFFFFF
+#define COLOR_YELLOW 0x00FFFF00
+
+typedef unsigned int  uint;
+typedef unsigned long ulong;
+typedef unsigned char uchar;
+
+struct gfx_context_t {
+	SDL_Window *window;
+	SDL_Renderer *renderer;
+	SDL_Texture *texture;
+	uint32_t *pixels;
+	int width;
+	int height;
+};
+
+extern void gfx_putpixel(struct gfx_context_t *ctxt, int x, int y, uint32_t color);
+extern void gfx_clear(struct gfx_context_t *ctxt, uint32_t color);
+extern struct gfx_context_t* gfx_create(char *text, uint width, uint height);
+extern void gfx_destroy(struct gfx_context_t *ctxt);
+extern void gfx_present(struct gfx_context_t *ctxt);
+extern SDL_Keycode gfx_keypressed();
+extern int gfx_left_mouse_pressed(point *p);
+extern int gfx_right_mouse_pressed(point *p);
+extern int gfx_left_right_mouse_pressed(point *p);
+extern SDL_EventType poll_event();
+
+#endif
diff --git a/lib/lbm.c b/lib/lbm.c
new file mode 100644
index 0000000..dad84b0
--- /dev/null
+++ b/lib/lbm.c
@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "gfx.h"
+
+#define MAXCOLOR 255
+
+void show_pixels(struct gfx_context_t* context, int nx, int ny) {
+    gfx_clear(context, COLOR_BLACK);
+
+    for(int ix = 0; ix < nx; ++ix) {
+		for(int iy = 0; iy < ny; ++iy){
+            int px = rand() % MAXCOLOR;
+
+            uint32_t color = MAKE_COLOR(px, px, px);
+
+            gfx_putpixel(context, ix, iy, color);
+        }
+    }
+    gfx_present(context);
+}
+
+int main()
+{ 
+    int nx = 600;
+    int ny = 400;
+    struct gfx_context_t* context = gfx_create("Random Image", nx, ny);
+    if (!context) {
+        fprintf(stderr, "Graphic mode initialization failed!\n");
+        EXIT_FAILURE;
+    }
+
+    SDL_ShowCursor(SDL_ENABLE); // needed to se the cursor
+    SDL_Keycode key_pressed = 0; // escape keyy needed
+
+    int max_iter = 100000;
+
+    int it = 0;
+    point p;
+    while (it < max_iter) { // press escape to exit the loop
+        SDL_PumpEvents();
+
+        key_pressed = gfx_keypressed();
+        if (key_pressed == SDLK_ESCAPE) {
+            break;
+        }
+
+        show_pixels(context, nx, ny);
+        int mask_id = gfx_left_right_mouse_pressed(&p);
+
+        it += 1;
+    }
+
+	return EXIT_SUCCESS;
+}
diff --git a/lib/point.h b/lib/point.h
new file mode 100644
index 0000000..0ce8448
--- /dev/null
+++ b/lib/point.h
@@ -0,0 +1,9 @@
+#ifndef _POINT_H_
+#define _POINT_H_
+
+typedef struct {
+	int x;
+	int y;
+} point;
+
+#endif
\ No newline at end of file
-- 
GitLab