-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.cpp
195 lines (151 loc) · 3.83 KB
/
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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdio.h>
#include <iostream>
#include "GUI.h"
#include "queue"
#include "kazuminlib/PPM.h"
#include "kazuminlib/BMP.h"
using namespace std;
using namespace kazumin;
//typedef void (GUI::*GUIFunc_i)(int);
unsigned char *g_img;
thread *g_hThread;
void close();
void passiveMotionCallback(int, int);
void test(int val){
// cout<<"test "<<val<<endl;
//PPMのテスト
PPM *ppm;
ppm = new PPM();
ppm->ChangeSize(640, 400);
ppm->LoadRGB(g_img, 640, 400);
ppm->Write("screenshot.ppm");
delete ppm;
//Bitmapのテスト
BMP *bmp;
bmp = new BMP();
bmp->ChangeSize(640, 400);
bmp->LoadRGB(g_img, 640, 400);
bmp->Write("screenshot.bmp");
delete bmp;
return;
}
std::queue<std::pair <int, int>> out_buf;
void keyboard_callback(unsigned char key, int x, int y){
out_buf.push(std::make_pair(key - '0', 1));
fprintf(stderr, "%d\n", key);
}
void mouse_callback(int x, int y){
out_buf.push(std::make_pair(0, 12));
out_buf.push(std::make_pair(x, 12));
out_buf.push(std::make_pair(y, 12));
fprintf(stderr, "x : %x\n", x);
fprintf(stderr, "y : %x\n", y);
}
void click_callback(int button, int status, int x, int y){
out_buf.push(std::make_pair(0, 12));
out_buf.push(std::make_pair(x, 12));
out_buf.push(std::make_pair(y, 12));
fprintf(stderr, "x : %x\n", x);
fprintf(stderr, "y : %x\n", y);
}
void GUI::ThreadProc(){
try{
// img = new unsigned char[scrnx * scrny *3];
//cout<<"a"<<endl;
int argc=1;
char *argv = new char[1];
glutInit(&argc, &argv); //fake command-line args
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(scrnx, scrny);
hMainWin = glutCreateWindow("display");
// gluOrtho2D(0.0, scrnx, 0.0, scrny);
glPixelZoom(1,-1);
//glutDisplayFunc(a::display); //message loopをglutMainLoopではなくwhileでやっていて、その中でdisplayを呼んでいるため必要ない(こうするためにはGui::displayをstaticメンバ関数にする必要がある)
// GUIFunc_i f = timer;
// glutTimerFunc(500, f, 0);
glutPassiveMotionFunc(passiveMotionCallback);
glutCloseFunc(close);
glutKeyboardFunc(keyboard_callback);
//glutMouseFunc(click_callback);
glutPassiveMotionFunc(mouse_callback);
int menu = glutCreateMenu(test);
glutAddMenuEntry("screenshot", 1);
glutAttachMenu(GLUT_RIGHT_BUTTON);
while(msgflg){ //message loop
glutMainLoopEvent();
this->display();
}
}
catch(char m){
glutDestroyWindow(hMainWin);
cout<<"destroy"<<endl;
return;
}
}
void passiveMotionCallback(int x, int y){
printf("passive motion");
}
void close(){
//goto gui_thread_end;
printf("close\n");
// g_hThread->terminate();
throw 'e';
}
void GUI::display(){
if(disp != NULL){
img = disp->Draw(); g_img = img;
}
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2f(-1,1);
// glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
if(img != NULL){
glDrawPixels(scrnx, scrny, GL_RGB, GL_UNSIGNED_BYTE, disp->Draw());
}
// glMatrixMode(GL_MODELVIEW);
glFlush();
}
/*
void GUI::timer(int val){
glutPostRedisplay();
}
*/
void GUI::init(){
disp = NULL;
img = NULL;
scrnx = DEFAULT_SCRNX;
scrny = DEFAULT_SCRNY;
msgflg = true;
}
GUI::GUI(){
init();
}
GUI::GUI(unsigned char *img){
init();
ChangeImgAddr(img);
}
GUI::GUI(Display *disp){
init();
ChangeDisplay(disp);
}
GUI::~GUI(){
msgflg = false;
hThread->detach();
delete hThread;
// delete img;
}
void GUI::OpenWindow(){
hThread = new thread(&GUI::ThreadProc, this); //メンバ関数を指定する場合は第2引数=this
g_hThread = hThread;
// hThread->join(); //とりあえず終わるまで待つ
}
void GUI::ChangeImgAddr(unsigned char *img){
if(img == NULL)
return;
this->img = img;
}
void GUI::ChangeDisplay(Display *disp){
if(disp == NULL) return;
this->disp = disp; //描画スレッドのほうで不整合が起きるのは今は気にしない
ChangeImgAddr(disp->img);
}