OSDN Git Service

[MOVIE_SAVER][WIP] Fixing incorrect frames at 60fps.
[csp-qt/common_source_project-fm7.git] / source / src / qt / gui / draw_thread.cpp
1 /*
2         Skelton for retropc emulator
3         Author : Takeda.Toshiya
4         Port to Qt : K.Ohta <whatisthis.sowhat _at_ gmail.com>
5         Date   : 2006.08.18 -
6         License : GPLv2
7         History : 2015.11.10 Split from qt_main.cpp
8         [ win32 main ] -> [ Qt main ] -> [Drawing]
9 */
10
11 #include <Qt>
12 #include <QApplication>
13 #include <QImage>
14 #include <QGuiApplication>
15
16 #include <SDL.h>
17 #include "emu.h"
18 #include "osd.h"
19 #include "vm/vm.h"
20
21 #include "qt_main.h"
22 #include "agar_logger.h"
23 #include "mainwidget_base.h"
24 #include "draw_thread.h"
25 #include "qt_glutil_gl2_0.h"
26
27 DrawThreadClass::DrawThreadClass(EMU *p, OSD *o, QObject *parent) : QThread(parent) {
28         MainWindow = (Ui_MainWindowBase *)parent;
29         glv = MainWindow->getGraphicsView();
30         p_emu = emu;
31         p_osd = o;
32         screen = QGuiApplication::primaryScreen();
33         
34         draw_screen_buffer = NULL;
35         
36         do_change_refresh_rate(screen->refreshRate());
37         connect(screen, SIGNAL(refreshRateChanged(qreal)), this, SLOT(do_change_refresh_rate(qreal)));
38         connect(this, SIGNAL(sig_update_screen(bitmap_t *)), glv, SLOT(update_screen(bitmap_t *)));
39         connect(this, SIGNAL(sig_push_frames_to_avio(int, int, int)), glv->extfunc, SLOT(paintGL_OffScreen(int, int, int)));
40         rec_frame_width = 640;
41         rec_frame_height = 480;
42         rec_frame_count = -1;
43
44         bDrawReq = false;
45 }
46
47 DrawThreadClass::~DrawThreadClass()
48 {
49 }
50
51 void DrawThreadClass::SetEmu(EMU *p)
52 {
53                 p_emu = p;
54                 p_osd = p->get_osd();
55 }
56
57
58 void DrawThreadClass::doDraw(bool flag)
59 {
60         p_osd->do_decode_movie(1);
61         if(flag) {
62                 draw_frames = p_osd->draw_screen();
63         } else {
64                 draw_frames = p_osd->no_draw_screen();
65         }
66         emit sig_draw_frames(draw_frames);
67 }
68
69 void DrawThreadClass::doExit(void)
70 {
71         //AGAR_DebugLog(AGAR_LOG_DEBUG, "DrawThread : Exit.");
72         bRunThread = false;
73         //this->exit(0);
74 }
75
76 void DrawThreadClass::doWork(const QString &param)
77 {
78         bRunThread = true;
79         do {
80                 if(bDrawReq) {
81                         if(draw_screen_buffer != NULL) {
82                                 bDrawReq = false;
83                                 emit sig_update_screen(draw_screen_buffer);
84                         }
85                 }
86                 if(rec_frame_count > 0) {
87                         emit sig_push_frames_to_avio(rec_frame_count,
88                                                                                  rec_frame_width, rec_frame_height);
89                         rec_frame_count = -1;
90                 }
91                 if(wait_count < 1.0f) {
92                         msleep(1);
93                         wait_count = wait_count + wait_refresh - 1.0f;
94                 } else {
95                         wait_factor = (int)wait_count;
96                         msleep(wait_factor);
97                         wait_count -= (qreal)wait_factor;
98                 }
99         } while(bRunThread);
100         AGAR_DebugLog(AGAR_LOG_DEBUG, "DrawThread : Exit.");
101         this->exit(0);
102 }
103
104 void DrawThreadClass::do_change_refresh_rate(qreal rate)
105 {
106         refresh_rate = rate;    
107         wait_refresh = 1000.0f / (refresh_rate * 4.0);
108         wait_count = wait_refresh * 1.0;
109 }
110
111 void DrawThreadClass::do_update_screen(bitmap_t *p)
112 {
113         draw_screen_buffer = p;
114         bDrawReq = true;
115 }
116         
117 void DrawThreadClass::do_req_encueue_video(int count, int width, int height)
118 {
119         rec_frame_width = width;
120         rec_frame_height = height;
121         rec_frame_count = count;
122 }