11#include " utils.hpp"
22
3+ #include < OpenGL/gl.h>
34#include < unordered_map>
5+ #include < cocos2d.h>
46
57std::string formatAddressIntoOffset (uintptr_t addr, bool module ) {
68 static std::unordered_map<uintptr_t , std::pair<std::string, std::string>> formatted;
@@ -16,4 +18,85 @@ std::string formatAddressIntoOffset(uintptr_t addr, bool module) {
1618 if (module ) return pair.first ;
1719 else return pair.second ;
1820 }
21+ }
22+
23+ std::vector<uint8_t > renderToBytes (cocos2d::CCNode* node, int & width, int & height) {
24+ // Get scale from cocos2d units to opengl units
25+ GLint viewport[4 ];
26+ glGetIntegerv (GL_VIEWPORT, viewport);
27+ auto winSize = cocos2d::CCDirector::sharedDirector ()->getWinSize ();
28+
29+ width = node->getContentSize ().width * (viewport[2 ] / winSize.width );
30+ height = node->getContentSize ().height * (viewport[3 ] / winSize.height );
31+
32+ // Create Texture
33+ GLuint texture;
34+ glGenTextures (1 , &texture);
35+ glBindTexture (GL_TEXTURE_2D, texture);
36+ glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGBA, width, height, 0 , GL_RGBA, GL_UNSIGNED_BYTE, NULL );
37+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
38+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
39+
40+ // Create Framebuffer Obejct
41+ GLuint fbo;
42+ glGenFramebuffers (1 , &fbo);
43+ glBindFramebuffer (GL_FRAMEBUFFER, fbo);
44+ glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0 );
45+
46+ glBindTexture (GL_TEXTURE_2D, 0 );
47+
48+ // Clear any data
49+ glClearColor (0 .0f , 0 .0f , 0 .0f , 0 .0f );
50+ glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
51+
52+ // Flip Y axis when projecting
53+ kmGLMatrixMode (KM_GL_PROJECTION);
54+ kmGLPushMatrix ();
55+ kmGLLoadIdentity ();
56+
57+ kmMat4 ortho;
58+ kmMat4OrthographicProjection (&ortho,
59+ 0 .0f , winSize.width , // left, right
60+ winSize.height , 0 .0f , // !!! Swap top and bottom
61+ -1 .0f , 1 .0f // near, far
62+ );
63+ kmGLMultMatrix (&ortho);
64+
65+ // Transform matrix so the node is drawn at 0,0
66+ kmGLMatrixMode (KM_GL_MODELVIEW);
67+ kmGLPushMatrix ();
68+ kmGLLoadIdentity ();
69+
70+ auto anchor = node->isIgnoreAnchorPointForPosition () ? ccp (0 , 0 ) : node->getAnchorPointInPoints ();
71+ kmGLTranslatef (
72+ anchor.x - node->getPositionX (),
73+ anchor.y - node->getPositionY () + (winSize.height - node->getContentSize ().height ),
74+ 0
75+ );
76+
77+ // Visit
78+ node->visit ();
79+
80+ // Undo matrix transformations
81+ kmGLPopMatrix ();
82+ kmGLMatrixMode (KM_GL_PROJECTION);
83+ kmGLPopMatrix ();
84+ kmGLMatrixMode (KM_GL_MODELVIEW);
85+
86+ // Read from Framebuffer
87+ std::vector<unsigned char > pixels (width * height * 4 ); // RGBA8
88+ glReadPixels (
89+ 0 , 0 , width, height,
90+ GL_RGBA, GL_UNSIGNED_BYTE,
91+ pixels.data ()
92+ );
93+
94+ // Unbind Framebuffer
95+ glBindFramebuffer (GL_FRAMEBUFFER, 0 );
96+
97+ // Delete
98+ glDeleteFramebuffers (1 , &fbo);
99+ glDeleteTextures (1 , &texture);
100+
101+ return pixels;
19102}
0 commit comments