11#include " utils.hpp"
22
3+ #if defined(GEODE_IS_MACOS)
4+ #include < OpenGL/gl.h>
5+ #elif defined(GEODE_IS_IOS)
6+ #include < OpenGLES/ES2/gl.h>
7+ #endif
38#include < unordered_map>
9+ #include < cocos2d.h>
10+
11+ using namespace cocos2d ;
412
513std::string formatAddressIntoOffset (uintptr_t addr, bool module ) {
614 static std::unordered_map<uintptr_t , std::pair<std::string, std::string>> formatted;
@@ -16,4 +24,92 @@ std::string formatAddressIntoOffset(uintptr_t addr, bool module) {
1624 if (module ) return pair.first ;
1725 else return pair.second ;
1826 }
27+ }
28+
29+ std::vector<uint8_t > renderToBytes (CCNode* node, int & width, int & height) {
30+ // Get scale from cocos2d units to opengl units
31+ GLint viewport[4 ];
32+ glGetIntegerv (GL_VIEWPORT, viewport);
33+ auto winSize = CCDirector::get ()->getWinSize ();
34+
35+ width = node->getContentSize ().width * (viewport[2 ] / winSize.width );
36+ height = node->getContentSize ().height * (viewport[3 ] / winSize.height );
37+
38+ // Create Texture
39+ GLuint texture;
40+ glGenTextures (1 , &texture);
41+ glBindTexture (GL_TEXTURE_2D, texture);
42+ glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGBA, width, height, 0 , GL_RGBA, GL_UNSIGNED_BYTE, NULL );
43+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
44+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
45+
46+ // Create Framebuffer Object
47+ GLuint fbo;
48+ glGenFramebuffers (1 , &fbo);
49+ glBindFramebuffer (GL_FRAMEBUFFER, fbo);
50+ glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0 );
51+
52+ // Unbind texture
53+ glBindTexture (GL_TEXTURE_2D, 0 );
54+
55+ // Clear any data
56+ glClearColor (0 .0f , 0 .0f , 0 .0f , 0 .0f );
57+ glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
58+
59+ // Flip Y when projecting
60+ kmGLMatrixMode (KM_GL_PROJECTION);
61+ kmGLPushMatrix ();
62+ kmGLLoadIdentity ();
63+
64+ kmMat4 ortho;
65+ kmMat4OrthographicProjection (&ortho,
66+ 0 .0f , winSize.width ,
67+ winSize.height , 0 .0f ,
68+ -1 .0f , 1 .0f
69+ );
70+ kmGLMultMatrix (&ortho);
71+
72+ // Transform matrix so the node is drawn at 0,0
73+ kmGLMatrixMode (KM_GL_MODELVIEW);
74+ kmGLPushMatrix ();
75+ kmGLLoadIdentity ();
76+
77+ auto anchor = node->isIgnoreAnchorPointForPosition () ? ccp (0 , 0 ) : node->getAnchorPointInPoints ();
78+ kmGLTranslatef (
79+ anchor.x - node->getPositionX (),
80+ anchor.y - node->getPositionY () + (winSize.height - node->getContentSize ().height ),
81+ 0
82+ );
83+
84+ // Visit
85+ node->visit ();
86+
87+ // Undo matrix transformations
88+ kmGLPopMatrix ();
89+ kmGLMatrixMode (KM_GL_PROJECTION);
90+ kmGLPopMatrix ();
91+ kmGLMatrixMode (KM_GL_MODELVIEW);
92+
93+ // Read from Framebuffer
94+ std::vector<unsigned char > pixels (width * height * 4 ); // RGBA8
95+ glReadPixels (
96+ 0 , 0 , width, height,
97+ GL_RGBA, GL_UNSIGNED_BYTE,
98+ pixels.data ()
99+ );
100+
101+ // Unbind Framebuffer
102+ glBindFramebuffer (GL_FRAMEBUFFER, 0 );
103+
104+ // Delete
105+ glDeleteFramebuffers (1 , &fbo);
106+ glDeleteTextures (1 , &texture);
107+
108+ return pixels;
109+ }
110+
111+ void saveRenderToFile (std::vector<uint8_t > const & data, int width, int height, char const * filename) {
112+ auto img = new CCImage ();
113+ img->initWithImageData ((void *)data.data (), data.size (), CCImage::kFmtRawData , width, height, 8 );
114+ img->saveToFile (filename, false );
19115}
0 commit comments