-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWinD.cpp
More file actions
143 lines (110 loc) · 3.02 KB
/
WinD.cpp
File metadata and controls
143 lines (110 loc) · 3.02 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// Created by Admin on 11/12/2020.
//
#include <SDL2/SDL.h>
#include "WinD.h"
#include <iostream>
#include <fstream>
using namespace std;
WinD::WinD() {
}
/**
* Initialize the SDL graphics engine
*
* @return Error value. 0 if no errors
*/
int WinD::initialize() {
if( SDL_Init( SDL_INIT_VIDEO ) != 0 ) {
cout << "SDL Init Error: " << SDL_GetError << endl;
return -1;
}
//Create the window
int dispCode = display();
setIcon();
return dispCode;
}
int WinD::setIcon() {
string iconPath = "C:\\Users\\Admin\\CLionProjects\\GraphingCalculator\\icon.bmp";
SDL_Surface* icon = SDL_LoadBMP(iconPath.c_str());
SDL_SetWindowIcon(win, icon);
SDL_FreeSurface(icon);
return 0;
}
int WinD::display() {
//Create Window
win = SDL_CreateWindow("Graphing Calculator", 100, 100, width, height, SDL_WINDOW_SHOWN);
if(win == nullptr){
cout << "SDL_CreateWindow Error: " << SDL_GetError << endl;
SDL_Quit();
return -2;
}
//Create SDL renderer
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(ren == nullptr){
SDL_DestroyWindow(win);
cout << "SDL_CreateRenderer Error: " << SDL_GetError << endl;
SDL_Quit();
return -3;
}
//Draw the background of the window
drawBackground();
return 0;
}
//Draw the background of the opened window gray
void WinD::drawBackground() {
//Set the color of the background
SDL_SetRenderDrawColor(ren, bkgColor, bkgColor, bkgColor, 255);
//Clear the window to re-render with new color
SDL_RenderClear(ren);
//Re-render with new color
SDL_RenderPresent(ren);
}
/**
* Display a segment in the window
*
* @param posX X position to display segment in
* @param posY Y position to display segment in
* @param width Width of the segment
* @param height Height of the segment
* @param colors Array of colors (r, g, b)
*
* @return code The exit code of the function
*/
int WinD::displaySegment(int posX, int posY, int width, int height, int color[]) {
/*int clearCode = SDL_RenderClear(ren);
if(clearCode != 0){
return -20;
}*/
int renderCode = SDL_SetRenderDrawColor(ren, color[0], color[1], color[2], 255);
if(renderCode != 0) {
return -21;
}
SDL_Rect* rect = new SDL_Rect();
rect->x = posX;
rect->y = posY;
rect->w = width;
rect->h = height;
int fillCode = SDL_RenderFillRect(ren, rect);
if(fillCode != 0){
return -22;
}
SDL_RenderPresent(ren);
cout << "drew" << endl;
delete rect;
//TODO Delete later
//SDL_Delay(50);
//windowClose();
return 0;
}
//Draw a line between two points
void WinD::drawLine(int x1, int y1, int x2, int y2, int colors[]){
SDL_SetRenderDrawColor(ren, colors[0], colors[1], colors[2], 255);
SDL_RenderDrawLine(ren, x1, y1, x2, y2);
SDL_RenderPresent(ren);
}
//Close the window
void WinD::windowClose() {
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
}