Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <SDL2/SDL.h>
- #define SCREEN_WIDTH 640
- #define SCREEN_HEIGHT 480
- #define true (1)
- #define false (0)
- typedef unsigned char bool;
- // Colour information
- typedef struct {
- uint8_t r;
- uint8_t g;
- uint8_t b;
- uint8_t a;
- } col_t;
- // Like SDL_Rect, but for a line
- typedef struct {
- int x1;
- int y1;
- int x2;
- int y2;
- } line_t;
- SDL_Window* window = NULL;
- SDL_Renderer* renderer = NULL;
- /* Function init
- * Initialises SDL, the window and the renderer and returns the success */
- bool
- init() {
- if (SDL_Init(SDL_INIT_VIDEO) < 0) {
- fprintf(stderr, "Error: SDL could not be initialised! SDL says: %s\n", SDL_GetError());
- return false;
- }
- // Try to set texture filtering to linear
- if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) {
- printf("Warning: the texture filtering could not be set to linear.");
- }
- // Create the window
- window = SDL_CreateWindow(
- "Asteroids",
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- SCREEN_WIDTH,
- SCREEN_HEIGHT,
- SDL_WINDOW_SHOWN
- );
- if (window == NULL) {
- fprintf(stderr, "Error: the window could not be created! SDL says: %s\n", SDL_GetError());
- return false;
- }
- // Create the renderer
- renderer = SDL_CreateRenderer(
- window,
- -1,
- SDL_RENDERER_ACCELERATED
- );
- if (renderer == NULL) {
- fprintf(stderr, "Error: the renderer could not be created! SDL says: %s\n", SDL_GetError());
- return false;
- }
- // Renderer colour
- if (
- SDL_SetRenderDrawColor(
- renderer,
- 0xFF, 0xFF, 0xFF, 0xFF
- ) < 0
- ) {
- fprintf(stderr, "Error: the renderer colour could not be set! SDL says: %s\n", SDL_GetError());
- return false;
- }
- return true;
- }
- /* Function draw_rect
- * Draws a rectangle with the points contained in rect and returns the success. */
- bool
- draw_rect(
- SDL_Rect* rect, // contains the points of the rectangle
- bool fill
- ) {
- if (fill) {
- if (SDL_RenderFillRect(renderer, rect) < 0) {
- return false;
- }
- } else {
- if (SDL_RenderDrawRect(renderer, rect) < 0) {
- return false;
- }
- }
- return true;
- }
- /* Function draw_line
- * Draws a line on the screen with the points contained in line */
- bool
- draw_line(line_t* line) {
- if (
- SDL_RenderDrawLine(
- renderer,
- line->x1,
- line->y1,
- line->x2,
- line->y2
- ) < 0
- ) {
- return false;
- }
- return true;
- }
- /* Function free_sdl
- * Free memory and quit SDL subsystems. */
- void
- free_sdl() {
- SDL_DestroyRenderer(renderer);
- renderer = NULL;
- SDL_DestroyWindow(window);
- window = NULL;
- SDL_Quit();
- }
- /* Function set_colour
- * Sets the render colour based off of the col_t struct passed in. */
- bool
- set_colour(col_t* col) {
- if (
- SDL_SetRenderDrawColor(
- renderer,
- col->r,
- col->g,
- col->b,
- col->a
- ) < 0
- ) {
- return false;
- }
- return true;
- }
- int
- main(void) {
- if (!init()) {
- return EXIT_FAILURE;
- }
- // Begin drawing
- if (SDL_RenderClear(renderer) < 0) {
- fprintf(stderr, "Error: could not clear the renderer! SDL says: %s\n", SDL_GetError());
- free_sdl();
- return EXIT_FAILURE;
- }
- // Red filled quad
- col_t col;
- col.r = 0xFF;
- col.g = 0x00;
- col.b = 0x00;
- col.a = 0xFF;
- if (!set_colour(&col)) {
- fprintf(stderr, "Error: could not set the renderer colour. SDL says: %s\n", SDL_GetError());
- free_sdl();
- return EXIT_FAILURE;
- }
- SDL_Rect rect;
- rect.x = SCREEN_WIDTH / 4;
- rect.y = SCREEN_HEIGHT / 4;
- rect.w = SCREEN_WIDTH / 2;
- rect.h = SCREEN_HEIGHT / 2;
- if (
- !draw_rect(
- &rect,
- true
- )
- ) {
- fprintf(stderr, "Error: could not draw the red filled quad. SDL says: %s\n", SDL_GetError());
- free_sdl();
- return EXIT_FAILURE;
- }
- // Outlined green quad
- col.r = 0x00;
- col.g = 0xFF;
- col.b = 0x00;
- col.a = 0xFF;
- if (!set_colour(&col)) {
- fprintf(stderr, "Error: could not set the renderer colour. SDL says: %s\n", SDL_GetError());
- free_sdl();
- return EXIT_FAILURE;
- }
- rect.x = SCREEN_WIDTH / 6;
- rect.y = SCREEN_HEIGHT / 6;
- rect.w = SCREEN_WIDTH * 2 / 3;
- rect.h = SCREEN_HEIGHT * 2 / 3;
- if (
- !draw_rect(
- &rect,
- false
- )
- ) {
- fprintf(stderr, "Error: could not draw the outline green quad. SDL says: %s\n", SDL_GetError());
- free_sdl();
- return EXIT_FAILURE;
- }
- // Blue horizontal line
- col.r = 0x00;
- col.g = 0x00;
- col.b = 0xFF;
- col.a = 0xFF;
- if (!set_colour(&col)) {
- fprintf(stderr, "Error: could not set the renderer colour. SDL says: %s\n", SDL_GetError());
- free_sdl();
- return EXIT_FAILURE;
- }
- line_t line;
- line.x1 = 0;
- line.y1 = SCREEN_HEIGHT / 2;
- line.x2 = SCREEN_WIDTH;
- line.y2 = SCREEN_HEIGHT / 2;
- if (!draw_line(&line)) {
- fprintf(stderr, "Error: could not draw the blue horizontal line. SDL says: %s\n", SDL_GetError());
- free_sdl();
- return EXIT_FAILURE;
- }
- // Vertical line of black dots
- col.r = 0x00;
- col.g = 0x00;
- col.b = 0x00;
- col.a = 0xFF;
- if (!set_colour(&col)) {
- fprintf(stderr, "Error: could not set the renderer colour. SDL says: %s\n", SDL_GetError());
- free_sdl();
- return EXIT_FAILURE;
- }
- for (int i = 0; i < SCREEN_HEIGHT; i += 4) {
- if (SDL_RenderDrawPoint(renderer, SCREEN_WIDTH / 2, i) < 0) {
- fprintf(stderr, "Error: could not one of the yellow points. SDL says: %s\n", SDL_GetError());
- free_sdl();
- return EXIT_FAILURE;
- }
- }
- SDL_RenderPresent(renderer); // Update the screen
- bool running = true;
- SDL_Event e;
- while (running) {
- // Handle events on queue
- while (SDL_PollEvent(&e) != 0) {
- if (e.type == SDL_QUIT) {
- running = false;
- }
- }
- }
- free_sdl();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement