@@ -39,7 +39,7 @@ void DevTools::newFrame() {
3939
4040 auto * director = CCDirector::sharedDirector ();
4141 const auto winSize = director->getWinSize ();
42- const auto frameSize = director->getOpenGLView ()->getFrameSize ();
42+ const auto frameSize = director->getOpenGLView ()->getFrameSize () * DevTools::retinaFactor () ;
4343
4444 // glfw new frame
4545 io.DisplaySize = ImVec2 (frameSize.width , frameSize.height );
@@ -76,7 +76,93 @@ void DevTools::render(GLRenderCtx* ctx) {
7676 this ->renderDrawData (ImGui::GetDrawData ());
7777}
7878
79+ bool DevTools::hasExtension (const std::string& ext) const {
80+ auto exts = reinterpret_cast <const char *>(glGetString (GL_EXTENSIONS));
81+ if (exts == nullptr ) {
82+ return false ;
83+ }
84+
85+ std::string extsStr (exts);
86+ log::debug (" {}" , extsStr);
87+ return extsStr.find (ext) != std::string::npos;
88+ }
89+
90+ namespace {
91+ static void drawTriangle (const std::array<CCPoint, 3 >& poli, const std::array<ccColor4F, 3 >& colors, const std::array<CCPoint, 3 >& uvs) {
92+ auto * shader = CCShaderCache::sharedShaderCache ()->programForKey (kCCShader_PositionTextureColor );
93+ shader->use ();
94+ shader->setUniformsForBuiltins ();
95+
96+ ccGLEnableVertexAttribs (kCCVertexAttribFlag_PosColorTex );
97+
98+ static_assert (sizeof (CCPoint) == sizeof (ccVertex2F), " so the cocos devs were right then" );
99+
100+ glVertexAttribPointer (kCCVertexAttrib_Position , 2 , GL_FLOAT, GL_FALSE, 0 , poli.data ());
101+ glVertexAttribPointer (kCCVertexAttrib_Color , 4 , GL_FLOAT, GL_FALSE, 0 , colors.data ());
102+ glVertexAttribPointer (kCCVertexAttrib_TexCoords , 2 , GL_FLOAT, GL_FALSE, 0 , uvs.data ());
103+
104+ glDrawArrays (GL_TRIANGLE_FAN, 0 , 3 );
105+ }
106+ }
107+
108+ void DevTools::renderDrawDataFallback (ImDrawData* draw_data) {
109+ glEnable (GL_SCISSOR_TEST);
110+
111+ const auto clip_scale = draw_data->FramebufferScale ;
112+
113+ for (int i = 0 ; i < draw_data->CmdListsCount ; ++i) {
114+ auto * list = draw_data->CmdLists [i];
115+ auto * idxBuffer = list->IdxBuffer .Data ;
116+ auto * vtxBuffer = list->VtxBuffer .Data ;
117+ for (auto & cmd : list->CmdBuffer ) {
118+ ccGLBindTexture2D (static_cast <GLuint>(reinterpret_cast <intptr_t >(cmd.GetTexID ())));
119+
120+ const auto rect = cmd.ClipRect ;
121+ const auto orig = toCocos (ImVec2 (rect.x , rect.y ));
122+ const auto end = toCocos (ImVec2 (rect.z , rect.w ));
123+ if (end.x <= orig.x || end.y >= orig.y )
124+ continue ;
125+ CCDirector::sharedDirector ()->getOpenGLView ()->setScissorInPoints (orig.x , end.y , end.x - orig.x , orig.y - end.y );
126+
127+ for (unsigned int i = 0 ; i < cmd.ElemCount ; i += 3 ) {
128+ const auto a = vtxBuffer[idxBuffer[cmd.IdxOffset + i + 0 ]];
129+ const auto b = vtxBuffer[idxBuffer[cmd.IdxOffset + i + 1 ]];
130+ const auto c = vtxBuffer[idxBuffer[cmd.IdxOffset + i + 2 ]];
131+ std::array<CCPoint, 3 > points = {
132+ toCocos (a.pos ),
133+ toCocos (b.pos ),
134+ toCocos (c.pos ),
135+ };
136+ static constexpr auto ccc4FromImColor = [](const ImColor color) {
137+ // beautiful
138+ return ccc4f (color.Value .x , color.Value .y , color.Value .z , color.Value .w );
139+ };
140+ std::array<ccColor4F, 3 > colors = {
141+ ccc4FromImColor (a.col ),
142+ ccc4FromImColor (b.col ),
143+ ccc4FromImColor (c.col ),
144+ };
145+
146+ std::array<CCPoint, 3 > uvs = {
147+ ccp (a.uv .x , a.uv .y ),
148+ ccp (b.uv .x , b.uv .y ),
149+ ccp (c.uv .x , c.uv .y ),
150+ };
151+
152+ drawTriangle (points, colors, uvs);
153+ }
154+ }
155+ }
156+
157+ glDisable (GL_SCISSOR_TEST);
158+ }
159+
79160void DevTools::renderDrawData (ImDrawData* draw_data) {
161+ static bool hasVaos = this ->hasExtension (" GL_ARB_vertex_array_object" );
162+ if (!hasVaos) {
163+ return this ->renderDrawDataFallback (draw_data);
164+ }
165+
80166 glEnable (GL_SCISSOR_TEST);
81167
82168 GLuint vao = 0 ;
@@ -173,7 +259,7 @@ class $modify(CCTouchDispatcher) {
173259 if (io.WantCaptureMouse ) {
174260 bool didGDSwallow = false ;
175261
176- if (shouldPassEventsToGDButTransformed ()) {
262+ if (DevTools::get ()-> shouldUseGDWindow () && shouldPassEventsToGDButTransformed ()) {
177263 auto win = ImGui::GetMainViewport ()->Size ;
178264 const auto gdRect = getGDWindowRect ();
179265 if (gdRect.Contains (pos) && !DevTools::get ()->pausedGame ()) {
@@ -208,7 +294,7 @@ class $modify(CCTouchDispatcher) {
208294 if (type != CCTOUCHMOVED) {
209295 io.AddMouseButtonEvent (0 , false );
210296 }
211- if (!DevTools::get ()->shouldPopGame ()) {
297+ if (!DevTools::get ()->shouldUseGDWindow () || ! DevTools::get ()-> shouldPopGame ()) {
212298 CCTouchDispatcher::touches (touches, event, type);
213299 }
214300 }
0 commit comments