Hmmmm and yours is working? This is how I do it:
main.mm:
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 | global_variable NSOpenGLContext *GlobalGLContext;
global_variable GLuint OpenGLDefaultInternalTextureFormat;
global_variable GLuint TextureId;
int main(int argc, const char * argv[])
{
//Setup OpenGL:
NSOpenGLPixelFormatAttribute openGLAttributes[] =
{
NSOpenGLPFAAccelerated,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 24,
0
};
NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes: openGLAttributes];
GlobalGLContext = [[NSOpenGLContext alloc] initWithFormat: format shareContext:NULL];
[format release];
GLint swapInt = 1;
[GlobalGLContext setValues: &swapInt forParameter: NSOpenGLCPSwapInterval];
[GlobalGLContext setView: [window contentView]];
[GlobalGLContext makeCurrentContext];
//SetupOpenGL:
glGenTextures(1, &TextureId);
OpenGLDefaultInternalTextureFormat = GL_RGBA8;
OpenGLDefaultInternalTextureFormat = 0x8C43; //GL_SRGB8_ALPHA8;
glEnable(GL_FRAMEBUFFER_SRGB);
while(running)
{
[GlobalGLContext makeCurrentContext];
ProcessEvents();
RenderWeirdGradient(&GlobalBackbuffer, XOffset, YOffset);
OpenGLDisplayBitmap(GlobalBackbuffer.Width, GlobalBackbuffer.Height,
GlobalBackbuffer.Memory, GlobalBackbuffer.Pitch,
WindowDimension.Width, WindowDimension.Height,
TextureId);
[GlobalGLContext flushBuffer];
}
}
|
This is the main part of my code. I don't have the View Delegate as it seems more "pure" to me. I got the idea from Jeff Bucks Minimal platform layer. I have an app delegate and a window delegate. The window gets created in the main function and the app and window delegates are set.
I only have one delegate which handles both application delegate and window delegate.
I think the OpenGL setup is done the right way. Though I just copied it also.
OpenGLDisplayBitmap:
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 | internal void OpenGLDisplayBitmap(int32 Width, int32 Height, void *Memory, int Pitch,
int32 WindowWidth, int32 WindowHeight, GLuint BlitTexture)
{
//Assert(Pitch == (Width*4));
glViewport(0, 0, Width, Height);
glDisable(GL_SCISSOR_TEST);
glBindTexture(GL_TEXTURE_2D, BlitTexture);
//
// glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, Width, Height, 0,
// GL_BGRA_EXT, GL_UNSIGNED_BYTE, GlobalBackbuffer.Memory);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, &GlobalBackbuffer.Memory);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_TEXTURE_2D);
glClearColor(1.0f, 0.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
OpenGLSetScreenspace(Width, Height);
// TODO(casey): Decide how we want to handle aspect ratio - black bars or crop?
union v2 MinP = {0, 0};
union v2 MaxP = {(r32)Width, (r32)Height};
union v4 Color = {1, 1, 1, 1};
OpenGLRectangle(MinP, MaxP, Color);
glBindTexture(GL_TEXTURE_2D, 0);
}
|
This is the same function as Casey's handmade hero has implemented. However it doesn't work. Everything compiles but I just get a pink screen for whatever reason. Litte do I know.
There are two more functions that are invoked in OpenGLDisplayBitmap: OpenGLSetScreenspace, OpenGLRectangle. However I think that do nothing but here is the code.
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 | inline void
OpenGLSetScreenspace(int32 Width, int32 Height)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
r32 a = SafeRatio1(2.0f, (r32)Width);
r32 b = SafeRatio1(2.0f, (r32)Height);
r32 Proj[] =
{
a, 0, 0, 0,
0, b, 0, 0,
0, 0, 1, 0,
-1, -1, 0, 1,
};
glLoadMatrixf(Proj);
}
inline void
OpenGLRectangle(union v2 MinP, union v2 MaxP, union v4 PremulColor, union v2 MinUV = V2(0, 0), v2 MaxUV = V2(1, 1))
{
glBegin(GL_TRIANGLES);
glColor4f(PremulColor.r, PremulColor.g, PremulColor.b, PremulColor.a);
// NOTE(casey): Lower triangle
glTexCoord2f(MinUV.x, MinUV.y);
glVertex2f(MinP.x, MinP.y);
glTexCoord2f(MaxUV.x, MinUV.y);
glVertex2f(MaxP.x, MinP.y);
glTexCoord2f(MaxUV.x, MaxUV.y);
glVertex2f(MaxP.x, MaxP.y);
// NOTE(casey): Upper triangle
glTexCoord2f(MinUV.x, MinUV.y);
glVertex2f(MinP.x, MinP.y);
glTexCoord2f(MaxUV.x, MaxUV.y);
glVertex2f(MaxP.x, MaxP.y);
glTexCoord2f(MinUV.x, MaxUV.y);
glVertex2f(MinP.x, MaxP.y);
glEnd();
}
|
Yeah however it's not working. I'm currently going through Casey's OpenGL section. I hope it helps to learn and understand it a little bit and hopefully implement an own more simplified version of this shenanigans. But if you see what I'm doing wrong then please tell me.
I just don't want the drawing where apple thinks I should do it. So I don't like using views they so much force a design pattern on you. Is there a C Mac OS X API? I heard there was something like Carbon but its deprecated now and its 32 bit. I can't find any documentations. Does it still work?
And finally, whats that weird beast mode syntax?
| inline void
OpenGLRectangle(union v2 MinP, union v2 MaxP, union v4 PremulColor, union v2 MinUV = V2(0, 0), v2 MaxUV = V2(1, 1))
{
|
Whats the political correct terminus to call this? I guess its C++?
One more thing: How fast will the Handmade Hero Engine/Game be when it's done? Like compared to stuff like Game Maker, Unity usw. Regarding 2D.