1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
#include <iostream>
#include <SDL2/SDL.h>
#include <stdio.h>
#include <string.h>
#include "Coordinate.h"
SDL_Renderer *renderer;
SDL_Texture *texture;
SDL_Surface *surface;
SDL_Window *window;
SDL_Event event;
Uint32* buffer;
const int HEIGHT = 500;
const int WIDTH = 500;
bool init(){
if(SDL_Init(SDL_INIT_EVERYTHING) < 0){
cout << "error" << endl;
return false;
}
window = SDL_CreateWindow("lines",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WIDTH,HEIGHT,SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTVSYNC);
texture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_STATIC,WIDTH,HEIGHT);
buffer = new Uint32[WIDTH * HEIGHT];
}
void setPixel(int x,int y,Uint8 red,Uint8 green,Uint8 blue){
Uint32 color = 0;
color += red;
color <<= 8;
color += green;
color <<= 8;
color+=blue;
color <<= 8;
color += 0xFF;
buffer[(y*WIDTH)+x] = color;
}
void render(){
SDL_UpdateTexture(texture,NULL,buffer,WIDTH*sizeof(Uint32));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture,NULL,NULL);
SDL_RenderPresent(renderer);
}
void cleanUp(){
SDL_DestroyRenderer(renderer);
renderer = NULL;
SDL_DestroyWindow(window);
window = NULL;
delete[] buffer;
buffer = NULL;
SDL_Quit();
}
int SDL_main(int argc,char* argv[])
{
init();
// set background to white
for(int y = 0; y < HEIGHT; y++){
for(int x = 0; x < WIDTH; x++){
setPixel(x,y,255,255,255);
}
}
Coordinate* coords[100];
// draw line
for(int i = 0; i < 100; i++){
coords[i] = new Coordinate("y=3x+10",i);
coords[i]->calculateY();
setPixel(coords[i]->x,coords[i]->y,50,120,140);
}
while(true){
SDL_PollEvent(&event);
if(event.type == SDL_QUIT){
break;
}
render();
}
cleanUp();
}
|