Last active
September 26, 2015 04:43
-
-
Save gldraphael/2009e1719e2fb4952f0d to your computer and use it in GitHub Desktop.
Simple Line Drawing Algorithm using SDL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SDL.h> | |
#include <stdio.h> | |
#include <string> | |
//Screen dimension constants | |
const int SCREEN_WIDTH = 640; | |
const int SCREEN_HEIGHT = 480; | |
//Starts up SDL and creates window | |
bool init(); | |
//Frees media and shuts down SDL | |
void close(); | |
// Draws the current frame | |
void draw(); | |
//The window we'll be rendering to | |
SDL_Window* gWindow = NULL; | |
//The window renderer | |
SDL_Renderer* gRenderer = NULL; | |
int main(int argc, char* args[]) | |
{ | |
//Start up SDL and create window | |
if (!init()) | |
{ | |
printf("Failed to initialize!\n"); | |
} | |
else | |
{ | |
//Main loop flag | |
bool quit = false; | |
//Event handler | |
SDL_Event e; | |
//While application is running | |
while (!quit) | |
{ | |
//Handle events on queue | |
while (SDL_PollEvent(&e) != 0) | |
{ | |
//User requests quit | |
if (e.type == SDL_QUIT) | |
{ | |
quit = true; | |
} | |
} | |
//Clear screen | |
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); | |
SDL_RenderClear(gRenderer); | |
// draw the frame | |
draw(); | |
//Update screen and wait for 100 ms | |
SDL_RenderPresent(gRenderer); | |
SDL_Delay(100); | |
} | |
} | |
//Free resources and close SDL | |
close(); | |
return 0; | |
} | |
bool init() | |
{ | |
//Initialize SDL | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) | |
{ | |
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError()); | |
return false; | |
} | |
//Set texture filtering to linear | |
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) | |
{ | |
printf("Warning: Linear texture filtering not enabled!"); | |
} | |
//Create window | |
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); | |
if (gWindow == NULL) | |
{ | |
printf("Window could not be created! SDL Error: %s\n", SDL_GetError()); | |
return false; | |
} | |
//Create renderer for window | |
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED); | |
if (gRenderer == NULL) | |
{ | |
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError()); | |
return false; | |
} | |
//Initialize renderer color | |
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); | |
return true; | |
} | |
void close() | |
{ | |
//Destroy window | |
SDL_DestroyRenderer(gRenderer); | |
SDL_DestroyWindow(gWindow); | |
gWindow = NULL; | |
gRenderer = NULL; | |
SDL_Quit(); | |
} | |
int sign(float args) | |
{ | |
if (args < 0) | |
return -1; | |
else if (args > 0) | |
return 1; | |
else return 0; | |
} | |
void draw() | |
{ | |
int x = 0, y = 0, x1 = SCREEN_WIDTH / 2, y1 = SCREEN_HEIGHT; | |
float l = abs(x1 - x) > abs(y1 - y) ? abs(x1 - x) : abs(y1 - y); | |
float dx = (x1 - x) / l; | |
float dy = (y1 - y) / l; | |
float xinc = x + 0.5 * sign(dx); | |
float yinc = y + 0.5 * sign(dy); | |
// Set Draw color to Red | |
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0x00, 0x00, 0xFF); | |
for (int i = 0; i <= l; i++) | |
{ | |
SDL_RenderDrawPoint(gRenderer, abs(xinc), abs(yinc)); | |
xinc = abs(xinc + dx); | |
yinc = abs(yinc + dy); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment