-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulation_gui.cpp
77 lines (63 loc) · 2.13 KB
/
simulation_gui.cpp
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
#include <iostream>
#include <SDL2/SDL.h>
#include "lib/SClib.cpp"
#define WINDOW_WIDTH 400
#define WINDOW_HEIGHT 300
#define WIDTH 128
#define HEIGHT 120
SDL_Rect SrcR;
SDL_Rect DestR;
void MOD_SDL_Init_Rect(SDL_Rect *srcrect, SDL_Rect *dstrect) {
srcrect->x = 0;
srcrect->y = 0;
srcrect->w = WIDTH;
srcrect->h = HEIGHT;
dstrect->x = WINDOW_WIDTH / 2 - WIDTH / 2;
dstrect->y = WINDOW_HEIGHT / 2 - HEIGHT / 2;
dstrect->w = WIDTH;
dstrect->h = HEIGHT;
}
int main(int argc, char *argv[]) {
serverSmartCar server;
server.connectServer();
SDL_Init(SDL_INIT_EVERYTHING);
MOD_SDL_Init_Rect(&SrcR, &DestR);
SDL_Window *window = SDL_CreateWindow("Simulator GUI", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) {
std::cout << "Could not create window: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer == NULL) {
std::cout << "Could not create renderer" << std::endl;
return 4;
}
SDL_SetRenderDrawColor(renderer, 0xCC, 0xCC, 0xCC, 0xFF);
int len = WIDTH * HEIGHT * 3, pitch = HEIGHT * 3;
char* pixels = new char[len];
// for (int i = 0; i < len; i++) {
// pixels[i] = 0;
// }
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
SDL_Event windowEvent;
int counter = 0;
while (true) {
if (SDL_PollEvent(&windowEvent)) {
if (windowEvent.type == SDL_QUIT) break;
}
SDL_LockTexture(texture, NULL, (void**) &pixels, &pitch);
server.getImg1D(pixels);
SDL_UnlockTexture(texture);
SDL_RenderClear(renderer);
SDL_UpdateTexture(texture, NULL, pixels, pitch);
SDL_RenderCopy(renderer, texture, &SrcR, &DestR);
SDL_RenderPresent(renderer);
counter = (counter + 3) % len;
SDL_Delay(100);
}
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}