Examples: iOS: Fixed missing call to CreateContext() + various shallow coding style tweaks. (#1835)

docking
omar 6 years ago
parent b88fbd69cc
commit 5fd23eeb74

@ -9,10 +9,8 @@
#import "debug_hud.h" #import "debug_hud.h"
#define BUFFER_OFFSET(i) ((char *)NULL + (i)) #define BUFFER_OFFSET(i) ((char *)NULL + (i))
#define SERVERNAME_KEY @"ServerName" #define SERVERNAME_KEY @"ServerName"
#define SERVERNAME_ALERT_TAG 10
#define SERVERNAME_ALERT_TAG (10)
// Uniform index. // Uniform index.
enum enum
@ -20,23 +18,22 @@ enum
UNIFORM_MODELVIEWPROJECTION_MATRIX, UNIFORM_MODELVIEWPROJECTION_MATRIX,
UNIFORM_NORMAL_MATRIX, UNIFORM_NORMAL_MATRIX,
UNIFORM_DIFFUSE_COLOR, UNIFORM_DIFFUSE_COLOR,
UNIFORM_COUNT_
NUM_UNIFORMS
}; };
GLint uniforms[NUM_UNIFORMS]; static GLint uniforms[UNIFORM_COUNT_];
// Attribute index. // Attribute index.
enum enum
{ {
ATTRIB_VERTEX, ATTRIB_VERTEX,
ATTRIB_NORMAL, ATTRIB_NORMAL,
NUM_ATTRIBUTES ATTRIB_COUNT_
}; };
GLfloat gCubeVertexData[216] = static const GLfloat gCubeVertexData[216] =
{ {
// Data layout for each line below is: // Data layout for each line below is:
// positionX, positionY, positionZ, normalX, normalY, normalZ, // pos x/y/z, normal x/y/z,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
@ -83,21 +80,17 @@ GLfloat gCubeVertexData[216] =
@interface GameViewController () <UIAlertViewDelegate> @interface GameViewController () <UIAlertViewDelegate>
{ {
GLuint _program; GLuint _program;
GLKMatrix4 _modelViewProjectionMatrix; GLKMatrix4 _modelViewProjectionMatrix;
GLKMatrix3 _normalMatrix; GLKMatrix3 _normalMatrix;
float _rotation; float _rotation;
GLuint _vertexArray; GLuint _vertexArray;
GLuint _vertexBuffer; GLuint _vertexBuffer;
DebugHUD _hud; DebugHUD _hud;
} }
@property (strong, nonatomic) EAGLContext* context; @property (strong, nonatomic) EAGLContext* context;
@property (strong, nonatomic) GLKBaseEffect* effect; @property (strong, nonatomic) GLKBaseEffect* effect;
@property (strong, nonatomic) ImGuiHelper* imgui; @property (strong, nonatomic) ImGuiHelper* imgui;
@property (weak, nonatomic) IBOutlet UIButton* btnServername; @property (weak, nonatomic) IBOutlet UIButton* btnServername;
@property (strong, nonatomic) NSString* serverName; @property (strong, nonatomic) NSString* serverName;
- (IBAction)onServernameTapped:(id)sender; - (IBAction)onServernameTapped:(id)sender;
@ -119,9 +112,8 @@ GLfloat gCubeVertexData[216] =
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!self.context) { if (!self.context)
NSLog(@"Failed to create ES context"); NSLog(@"Failed to create ES context");
}
GLKView *view = (GLKView *)self.view; GLKView *view = (GLKView *)self.view;
view.context = self.context; view.context = self.context;
@ -147,23 +139,22 @@ GLfloat gCubeVertexData[216] =
{ {
[self tearDownGL]; [self tearDownGL];
if ([EAGLContext currentContext] == self.context) { if ([EAGLContext currentContext] == self.context)
[EAGLContext setCurrentContext:nil]; [EAGLContext setCurrentContext:nil];
} }
}
- (void)didReceiveMemoryWarning - (void)didReceiveMemoryWarning
{ {
[super didReceiveMemoryWarning]; [super didReceiveMemoryWarning];
if ([self isViewLoaded] && ([[self view] window] == nil)) { if ([self isViewLoaded] && ([[self view] window] == nil))
{
self.view = nil; self.view = nil;
[self tearDownGL]; [self tearDownGL];
if ([EAGLContext currentContext] == self.context) { if ([EAGLContext currentContext] == self.context)
[EAGLContext setCurrentContext:nil]; [EAGLContext setCurrentContext:nil];
}
self.context = nil; self.context = nil;
} }
@ -171,7 +162,8 @@ GLfloat gCubeVertexData[216] =
} }
- (BOOL)prefersStatusBarHidden { - (BOOL)prefersStatusBarHidden
{
return YES; return YES;
} }
@ -193,7 +185,8 @@ GLfloat gCubeVertexData[216] =
BOOL serverNameWasSet = self.serverName.length > 0; BOOL serverNameWasSet = self.serverName.length > 0;
NSString *serverName = [[alertView textFieldAtIndex:0] text]; NSString *serverName = [[alertView textFieldAtIndex:0] text];
if ([serverName length] > 0) { if ([serverName length] > 0)
{
self.serverName = serverName; self.serverName = serverName;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:serverName forKey:SERVERNAME_KEY ]; [userDefaults setObject:serverName forKey:SERVERNAME_KEY ];
@ -202,7 +195,8 @@ GLfloat gCubeVertexData[216] =
[self.btnServername setTitle:self.serverName forState:UIControlStateNormal]; [self.btnServername setTitle:self.serverName forState:UIControlStateNormal];
// If we hadn't previously connected, try now // If we hadn't previously connected, try now
if (!serverNameWasSet) { if (!serverNameWasSet)
{
[self.imgui connectServer:self.serverName]; [self.imgui connectServer:self.serverName];
} }
else else
@ -241,8 +235,6 @@ GLfloat gCubeVertexData[216] =
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12)); glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12));
glBindVertexArrayOES(0); glBindVertexArrayOES(0);
} }
- (void)tearDownGL - (void)tearDownGL
@ -254,7 +246,8 @@ GLfloat gCubeVertexData[216] =
self.effect = nil; self.effect = nil;
if (_program) { if (_program)
{
glDeleteProgram(_program); glDeleteProgram(_program);
_program = 0; _program = 0;
} }
@ -285,9 +278,7 @@ GLfloat gCubeVertexData[216] =
modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix); modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);
_normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(modelViewMatrix), NULL); _normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(modelViewMatrix), NULL);
_modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix); _modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix);
_rotation += self.timeSinceLastUpdate * (_hud.rotation_speed * (M_PI / 180.0)); _rotation += self.timeSinceLastUpdate * (_hud.rotation_speed * (M_PI / 180.0));
} }
@ -359,7 +350,8 @@ GLfloat gCubeVertexData[216] =
glBindAttribLocation(_program, GLKVertexAttribNormal, "normal"); glBindAttribLocation(_program, GLKVertexAttribNormal, "normal");
// Link program. // Link program.
if (![self linkProgram:_program]) { if (![self linkProgram:_program])
{
NSLog(@"Failed to link program: %d", _program); NSLog(@"Failed to link program: %d", _program);
if (vertShader) { if (vertShader) {
@ -384,11 +376,13 @@ GLfloat gCubeVertexData[216] =
uniforms[UNIFORM_DIFFUSE_COLOR] = glGetUniformLocation(_program, "diffuseColor"); uniforms[UNIFORM_DIFFUSE_COLOR] = glGetUniformLocation(_program, "diffuseColor");
// Release vertex and fragment shaders. // Release vertex and fragment shaders.
if (vertShader) { if (vertShader)
{
glDetachShader(_program, vertShader); glDetachShader(_program, vertShader);
glDeleteShader(vertShader); glDeleteShader(vertShader);
} }
if (fragShader) { if (fragShader)
{
glDetachShader(_program, fragShader); glDetachShader(_program, fragShader);
glDeleteShader(fragShader); glDeleteShader(fragShader);
} }
@ -402,7 +396,8 @@ GLfloat gCubeVertexData[216] =
const GLchar *source; const GLchar *source;
source = (GLchar *)[[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil] UTF8String]; source = (GLchar *)[[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil] UTF8String];
if (!source) { if (!source)
{
NSLog(@"Failed to load vertex shader"); NSLog(@"Failed to load vertex shader");
return NO; return NO;
} }
@ -423,7 +418,8 @@ GLfloat gCubeVertexData[216] =
#endif #endif
glGetShaderiv(*shader, GL_COMPILE_STATUS, &status); glGetShaderiv(*shader, GL_COMPILE_STATUS, &status);
if (status == 0) { if (status == 0)
{
glDeleteShader(*shader); glDeleteShader(*shader);
return NO; return NO;
} }
@ -439,7 +435,8 @@ GLfloat gCubeVertexData[216] =
#if defined(DEBUG) #if defined(DEBUG)
GLint logLength; GLint logLength;
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength); glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) { if (logLength > 0)
{
GLchar *log = (GLchar *)malloc(logLength); GLchar *log = (GLchar *)malloc(logLength);
glGetProgramInfoLog(prog, logLength, &logLength, log); glGetProgramInfoLog(prog, logLength, &logLength, log);
NSLog(@"Program link log:\n%s", log); NSLog(@"Program link log:\n%s", log);
@ -448,10 +445,8 @@ GLfloat gCubeVertexData[216] =
#endif #endif
glGetProgramiv(prog, GL_LINK_STATUS, &status); glGetProgramiv(prog, GL_LINK_STATUS, &status);
if (status == 0) { if (status == 0)
return NO; return NO;
}
return YES; return YES;
} }
@ -469,10 +464,8 @@ GLfloat gCubeVertexData[216] =
} }
glGetProgramiv(prog, GL_VALIDATE_STATUS, &status); glGetProgramiv(prog, GL_VALIDATE_STATUS, &status);
if (status == 0) { if (status == 0)
return NO; return NO;
}
return YES; return YES;
} }

@ -278,7 +278,6 @@ void ImGui_KeyboardCallback(uSynergyCookie cookie, uint16_t key,
int charForKeycode = (modifiers & USYNERGY_MODIFIER_SHIFT) ? g_keycodeCharShifted[scanCode] : g_keycodeCharUnshifted[scanCode]; int charForKeycode = (modifiers & USYNERGY_MODIFIER_SHIFT) ? g_keycodeCharShifted[scanCode] : g_keycodeCharUnshifted[scanCode];
io.AddInputCharacter((unsigned short)charForKeycode); io.AddInputCharacter((unsigned short)charForKeycode);
} }
} }
void ImGui_JoystickCallback(uSynergyCookie cookie, uint8_t joyNum, uint16_t buttons, int8_t leftStickX, int8_t leftStickY, int8_t rightStickX, int8_t rightStickY) void ImGui_JoystickCallback(uSynergyCookie cookie, uint8_t joyNum, uint16_t buttons, int8_t leftStickX, int8_t leftStickY, int8_t rightStickX, int8_t rightStickY)
@ -461,6 +460,7 @@ void ImGui_ClipboardCallback(uSynergyCookie cookie, enum uSynergyClipboardFormat
- (void)setupImGuiHooks - (void)setupImGuiHooks
{ {
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
[self setupKeymaps]; [self setupKeymaps];
@ -532,9 +532,8 @@ void ImGui_ClipboardCallback(uSynergyCookie cookie, enum uSynergyClipboardFormat
// Create a background thread for synergy // Create a background thread for synergy
_synergyQueue = dispatch_queue_create( "imgui-usynergy", NULL ); _synergyQueue = dispatch_queue_create( "imgui-usynergy", NULL );
dispatch_async( _synergyQueue, ^{ dispatch_async( _synergyQueue, ^{
while (1) { while (1)
uSynergyUpdate(&_synergyCtx); uSynergyUpdate(&_synergyCtx);
}
}); });
} }
@ -572,9 +571,7 @@ void ImGui_ClipboardCallback(uSynergyCookie cookie, enum uSynergyClipboardFormat
ImGuiStyle& style = ImGui::GetStyle(); ImGuiStyle& style = ImGui::GetStyle();
if (!g_FontTexture) if (!g_FontTexture)
{
ImGui_ImplIOS_CreateDeviceObjects(); ImGui_ImplIOS_CreateDeviceObjects();
}
io.DisplaySize = ImVec2(_view.bounds.size.width, _view.bounds.size.height); io.DisplaySize = ImVec2(_view.bounds.size.width, _view.bounds.size.height);
@ -584,9 +581,7 @@ void ImGui_ClipboardCallback(uSynergyCookie cookie, enum uSynergyClipboardFormat
style.TouchExtraPadding = ImVec2(0.0, 0.0); style.TouchExtraPadding = ImVec2(0.0, 0.0);
io.MousePos = ImVec2(g_mousePosX, g_mousePosY); io.MousePos = ImVec2(g_mousePosX, g_mousePosY);
for (int i=0; i < 3; i++) for (int i=0; i < 3; i++)
{
io.MouseDown[i] = g_MousePressed[i]; io.MouseDown[i] = g_MousePressed[i];
}
// This is an arbitrary scaling factor that works for me. Not sure what units these mousewheel values from synergy are supposed to be in. // This is an arbitrary scaling factor that works for me. Not sure what units these mousewheel values from synergy are supposed to be in.
io.MouseWheel = g_mouseWheelY / 500.0; io.MouseWheel = g_mouseWheelY / 500.0;

Loading…
Cancel
Save