start
This commit is contained in:
31
Makefile
Normal file
31
Makefile
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
CC := gcc
|
||||||
|
CFLAGS := -Wall -Wextra -g -fsanitize=address,undefined $(shell sdl2-config --cflags)
|
||||||
|
LDFLAGS := $(shell sdl2-config --libs)
|
||||||
|
TARGET := main
|
||||||
|
|
||||||
|
SRC_DIR := src
|
||||||
|
BLD_DIR := build
|
||||||
|
|
||||||
|
SRCS := $(filter-out $(SRC_DIR)/main.c, $(wildcard $(SRC_DIR)/*.c))
|
||||||
|
OBJS := $(patsubst $(SRC_DIR)/%.c, $(BLD_DIR)/%.o, $(SRCS))
|
||||||
|
|
||||||
|
.PHONY: all clean fclean re
|
||||||
|
|
||||||
|
all: $(BLD_DIR) $(TARGET)
|
||||||
|
|
||||||
|
$(BLD_DIR):
|
||||||
|
mkdir -p $(BLD_DIR)
|
||||||
|
|
||||||
|
$(TARGET): $(OBJS) $(SRC_DIR)/main.c
|
||||||
|
$(CC) $(CFLAGS) $(SRC_DIR)/main.c $(OBJS) -o $(TARGET) $(LDFLAGS)
|
||||||
|
|
||||||
|
$(BLD_DIR)/%.o: $(SRC_DIR)/%.c | $(BLD_DIR)
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(BLD_DIR)
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
rm -f $(TARGET)
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
71
src/main.c
Normal file
71
src/main.c
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
#include "utils.h"
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define SEED (42)
|
||||||
|
#define WIDTH (800)
|
||||||
|
#define HEIGHT (600)
|
||||||
|
|
||||||
|
t_flags process_args(int argc, char **argv) {
|
||||||
|
t_flags output_flags = {0};
|
||||||
|
if (argc <= 1)
|
||||||
|
return output_flags;
|
||||||
|
if (argv[1][0] != '-')
|
||||||
|
return output_flags;
|
||||||
|
|
||||||
|
char *flags = argv[1];
|
||||||
|
int i = 1;
|
||||||
|
char c = flags[i];
|
||||||
|
while (c) {
|
||||||
|
if (c == 'g')
|
||||||
|
output_flags.graphical = 1;
|
||||||
|
if (c == 'm')
|
||||||
|
output_flags.multithread = 1;
|
||||||
|
if (c == 'o')
|
||||||
|
output_flags.opti = 1;
|
||||||
|
c = flags[++i];
|
||||||
|
}
|
||||||
|
return output_flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
t_flags flags = process_args(argc, argv);
|
||||||
|
if (flags.graphical) {
|
||||||
|
srand(SEED);
|
||||||
|
|
||||||
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
|
|
||||||
|
SDL_Window *win = SDL_CreateWindow("pixels", 0, 0, WIDTH, HEIGHT, 0);
|
||||||
|
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
|
||||||
|
|
||||||
|
SDL_Texture *tex =
|
||||||
|
SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888,
|
||||||
|
SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
|
||||||
|
|
||||||
|
uint32_t pixels[HEIGHT][WIDTH];
|
||||||
|
for (int y = 0; y < HEIGHT; y++)
|
||||||
|
for (int x = 0; x < WIDTH; x++)
|
||||||
|
pixels[y][x] = 0xFFFFFF00;
|
||||||
|
int running = 1;
|
||||||
|
SDL_Event e;
|
||||||
|
while (running) {
|
||||||
|
while (SDL_PollEvent(&e))
|
||||||
|
if (e.type == SDL_QUIT)
|
||||||
|
running = 0;
|
||||||
|
|
||||||
|
// Envoie la matrice à la texture
|
||||||
|
SDL_UpdateTexture(tex, NULL, pixels, WIDTH * sizeof(uint32_t));
|
||||||
|
|
||||||
|
SDL_RenderClear(ren);
|
||||||
|
SDL_RenderCopy(ren, tex, NULL, NULL);
|
||||||
|
SDL_RenderPresent(ren);
|
||||||
|
}
|
||||||
|
SDL_DestroyTexture(tex);
|
||||||
|
SDL_DestroyRenderer(ren);
|
||||||
|
SDL_DestroyWindow(win);
|
||||||
|
SDL_Quit();
|
||||||
|
}
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
12
src/utils.h
Normal file
12
src/utils.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef UTILS_H
|
||||||
|
#define UTILS_H
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int graphical;
|
||||||
|
int multithread;
|
||||||
|
int opti;
|
||||||
|
} t_flags;
|
||||||
|
|
||||||
|
t_flags process_args(int argc, char *argv[]);
|
||||||
|
|
||||||
|
#endif // !UTILS_H
|
||||||
Reference in New Issue
Block a user