aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvimene <vincent.menegaux@gmail.com>2023-11-18 06:08:56 +0100
committervimene <vincent.menegaux@gmail.com>2023-11-18 06:08:56 +0100
commit80cd68f89f5abb6a663555ed9dbd01cc4baa8137 (patch)
tree813a8b41f6358d5c856f389650b10b14473e63c2
parent3dc7f970305480bf5c4f60c21416adfc0b8dc958 (diff)
downloadengine-80cd68f89f5abb6a663555ed9dbd01cc4baa8137.tar.gz
started working on a pixel window
-rw-r--r--Makefile2
-rw-r--r--main.cpp60
2 files changed, 60 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 04d6739..4ba6469 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ CC=g++
LD=g++
COMMON_ARGS=-Wall -Wextra
CC_ARGS=$(COMMON_ARGS)
-LD_ARGS=$(COMMON_ARGS) -lncurses
+LD_ARGS=$(COMMON_ARGS) -lncurses -lSDL2
.PHONY: clean
diff --git a/main.cpp b/main.cpp
index 836418c..eb8f117 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,6 +1,9 @@
#include <iostream>
#include <ncurses.h>
#include <cmath>
+#include <cstdint>
+#include <vector>
+#include <SDL2/SDL.h>
#include "chfb.h"
#include "obj3d.h"
#include "math_vector.h"
@@ -19,7 +22,7 @@
#define PI 3.1415926535f
-int main() {
+void main_term() {
// init
setlocale(LC_ALL, "");
initscr();
@@ -253,5 +256,60 @@ int main() {
// terminate
endwin();
+}
+
+#define SCREEN_WIDTH 640
+#define SCREEN_HEIGHT 480
+
+void main_SDL() {
+ SDL_Window* window = NULL;
+ SDL_Renderer* renderer = NULL;
+ SDL_Texture* texture = NULL;
+
+ if (SDL_Init(SDL_INIT_VIDEO) < 0) {
+ fprintf(stderr, "Error: SDL_Init error: %s\n", SDL_GetError());
+ return;
+ }
+ window = SDL_CreateWindow("Engine", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
+ if (window == NULL) {
+ fprintf(stderr, "Error: SDL_CreateWindow error: %s\n", SDL_GetError());
+ return;
+ }
+ renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
+ texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
+ std::vector<uint32_t> pixels(SCREEN_WIDTH * SCREEN_HEIGHT);
+ for (int y = 0; y < SCREEN_HEIGHT; y++) {
+ for (int x = 0; x < SCREEN_WIDTH; x++) {
+ pixels[x + y * SCREEN_WIDTH] = ((uint32_t) (x + y * SCREEN_WIDTH) << 8) | 0xFF;
+ }
+ }
+ SDL_UpdateTexture(texture, NULL, pixels.data(), SCREEN_WIDTH * 4);
+ SDL_RenderClear(renderer);
+ SDL_RenderCopy(renderer, texture, NULL, NULL);
+ SDL_RenderPresent(renderer);
+ SDL_UpdateWindowSurface(window);
+ SDL_Event e;
+ bool quit = false;
+ while (!quit) {
+ while (SDL_PollEvent(&e)) {
+ if (e.type == SDL_QUIT) {
+ quit = true;
+ }
+ }
+ }
+ SDL_DestroyTexture(texture);
+ SDL_DestroyRenderer(renderer);
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+}
+
+int main(int argc, char *argv[]) {
+ (void) argc;
+ (void) argv;
+#if 0
+ main_term();
+#else
+ main_SDL();
+#endif
return EXIT_SUCCESS;
}