Skip to content

Instantly share code, notes, and snippets.

@harieamjari
Created July 24, 2023 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harieamjari/9913db086a3f1c34e9d65dac67eaf3ce to your computer and use it in GitHub Desktop.
Save harieamjari/9913db086a3f1c34e9d65dac67eaf3ce to your computer and use it in GitHub Desktop.
/* gcc wmain2.c tracer.c utils.c math.c -fopenmp -lm -lmingw32 -lSDL2main -lSDL2
* -mwindows -Dmain=SDL_main -o wmain2 */
#include "common.h"
#include <SDL2/SDL.h>
#include <assert.h>
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct rgbai_t rgbai_t;
struct rgbai_t {
uint8_t r, g, b, a;
};
rgbai_t **img_buf;
extern rgba_t get_pixel(int, int, uint64_t *);
int image_width, image_height;
scene3D scene = {0};
void putpixel(SDL_Surface *surface, int x, int y, uint8_t r, uint8_t g,
uint8_t b) {
/* Here p is the address to the pixel we want to set */
uint32_t *p = (uint32_t *)surface->pixels + y * surface->w + x;
*p = SDL_MapRGB(surface->format, r, g, b);
}
char render_finished = 0;
void render_scene(void *p) {
#pragma omp parallel for
for (int y = 0; y < image_height; y++) {
uint64_t rng = y;
for (int x = 0; x < image_width; x++) {
rgba_t rgba =
get_pixel(x - (image_width / 2), (image_height / 2) - y, &rng);
img_buf[y][x].r = (uint8_t)(rgba.r * 255.0);
img_buf[y][x].g = (uint8_t)(rgba.g * 255.0);
img_buf[y][x].b = (uint8_t)(rgba.b * 255.0);
img_buf[y][x].a = (uint8_t)(rgba.a * 255.0);
}
}
render_finished = 1;
}
int main(int argc, char *argv[]) {
if (argc == 3) {
image_width = atoi(argv[1]);
image_height = atoi(argv[2]);
} else {
image_width = 640;
image_height = 480;
}
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr,
"SDL could not be initialized!\n"
"SDL_Error: %s\n",
SDL_GetError());
return 0;
}
SDL_Window *window = NULL;
SDL_Surface *surface = NULL;
window = SDL_CreateWindow("Raytracer", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, image_width, image_height,
SDL_WINDOW_SHOWN);
if (window == NULL) {
fprintf(stderr, "Window could not be created: %s\n", SDL_GetError());
return 1;
}
surface = SDL_GetWindowSurface(window);
SDL_Event event;
img_buf = malloc(sizeof(rgbai_t *) * image_height);
assert(img_buf != NULL);
for (int y = 0; y < image_height; y++)
img_buf[y] = calloc(sizeof(rgbai_t), image_width);
// srand(time(NULL));
#include "scene.c"
_beginthread(render_scene, 1024 * 1024 * 20, NULL);
/* while render is not finished */
while (!render_finished) {
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
break;
//SDL_Delay(1500);
//SDL_LockSurface(surface);
for (int y = 0; y < image_height; y++)
for (int x = 0; x < image_width; x++) {
putpixel(surface, x, y, img_buf[y][x].r, img_buf[y][x].g,
img_buf[y][x].b);
}
//SDL_UnlockSurface(surface);
SDL_UpdateWindowSurface(window);
}
for (int y = 0; y < image_height; y++)
free(img_buf[y]);
free(img_buf);
while (1) {
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
break;
}
SDL_FreeSurface(surface);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment