Set up initial rendering
This commit is contained in:
		| @@ -77,11 +77,11 @@ | |||||||
|       "-x", |       "-x", | ||||||
|       "c", |       "c", | ||||||
|       "-o", |       "-o", | ||||||
|       "/tmp/main-b9c948.o", |       "/tmp/main-7469c3.o", | ||||||
|       "main.c" |       "main.c" | ||||||
|     ], |     ], | ||||||
|     "directory": "/home/abdelrahman/Sources/programming/starfield", |     "directory": "/home/abdelrahman/Sources/programming/starfield", | ||||||
|     "file": "/home/abdelrahman/Sources/programming/starfield/main.c", |     "file": "/home/abdelrahman/Sources/programming/starfield/main.c", | ||||||
|     "output": "/tmp/main-b9c948.o" |     "output": "/tmp/main-7469c3.o" | ||||||
|   } |   } | ||||||
| ] | ] | ||||||
|   | |||||||
							
								
								
									
										90
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,90 @@ | |||||||
|  | #include <SDL2/SDL.h> | ||||||
|  | #include <SDL2/SDL_pixels.h> | ||||||
|  | #include <SDL2/SDL_rect.h> | ||||||
|  | #include <SDL2/SDL_render.h> | ||||||
|  | #include <SDL2/SDL_surface.h> | ||||||
|  | #include <SDL2/SDL_video.h> | ||||||
|  | #include <stdbool.h> | ||||||
|  | #include <stdint.h> | ||||||
|  | #include <stdio.h> | ||||||
|  | #include <stdlib.h> | ||||||
|  | #include <string.h> | ||||||
|  |  | ||||||
|  | #define WINDOW_WIDTH 800 | ||||||
|  | #define WINDOW_HEIGHT 600 | ||||||
|  |  | ||||||
|  | #define BG_COLOR 0x060616ff | ||||||
|  |  | ||||||
|  | void render_clear(SDL_Surface *surface, uint32_t color); | ||||||
|  | void fill_rect(SDL_Surface *surface, uint32_t x, uint32_t y, uint32_t w, | ||||||
|  |                uint32_t h, uint32_t color); | ||||||
|  |  | ||||||
|  | int main(void) { | ||||||
|  |   SDL_Init(SDL_INIT_EVERYTHING); | ||||||
|  |  | ||||||
|  |   SDL_Window *window = SDL_CreateWindow("Starfield", SDL_WINDOWPOS_CENTERED, | ||||||
|  |                                         SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, | ||||||
|  |                                         WINDOW_HEIGHT, SDL_WINDOW_SHOWN); | ||||||
|  |  | ||||||
|  |   SDL_Surface *surface = SDL_GetWindowSurface(window); | ||||||
|  |  | ||||||
|  |   SDL_Surface *canvas = SDL_CreateRGBSurfaceWithFormat( | ||||||
|  |       0, surface->w, surface->h, 32, SDL_PIXELFORMAT_ABGR32); | ||||||
|  |  | ||||||
|  |   bool running = true; | ||||||
|  |  | ||||||
|  |   SDL_Event event = {0}; | ||||||
|  |  | ||||||
|  |   while (running) { | ||||||
|  |     while (SDL_PollEvent(&event)) { | ||||||
|  |       switch (event.type) { | ||||||
|  |       case SDL_QUIT: | ||||||
|  |         running = false; | ||||||
|  |         break; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     render_clear(canvas, BG_COLOR); | ||||||
|  |  | ||||||
|  |     SDL_BlitSurface(canvas, NULL, surface, NULL); | ||||||
|  |  | ||||||
|  |     SDL_UpdateWindowSurface(window); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SDL_DestroyWindow(window); | ||||||
|  |  | ||||||
|  |   SDL_Quit(); | ||||||
|  |  | ||||||
|  |   return EXIT_SUCCESS; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void render_clear(SDL_Surface *surface, uint32_t color) { | ||||||
|  |   uint32_t length = surface->w * surface->h; | ||||||
|  |  | ||||||
|  |   SDL_LockSurface(surface); | ||||||
|  |  | ||||||
|  |   uint32_t *pixels = (uint32_t *)(surface->pixels); | ||||||
|  |  | ||||||
|  |   for (uint32_t i = 0; i < length; ++i) { | ||||||
|  |     pixels[i] = color; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SDL_UnlockSurface(surface); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void fill_rect(SDL_Surface *surface, uint32_t x, uint32_t y, uint32_t w, | ||||||
|  |                uint32_t h, uint32_t color) { | ||||||
|  |   SDL_LockSurface(surface); | ||||||
|  |  | ||||||
|  |   uint32_t *pixels = (uint32_t *)(surface->pixels); | ||||||
|  |  | ||||||
|  |   for (uint32_t row = 0; row < h; ++row) { | ||||||
|  |     for (uint32_t col = 0; col < w; ++col) { | ||||||
|  |       uint32_t i = (y + row) * surface->w + (x + col); | ||||||
|  |  | ||||||
|  |       pixels[i] = color; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SDL_UnlockSurface(surface); | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user