Archived
1
Fork 0
This repository has been archived on 2025-04-09. You can view files and clone it, but cannot push or open issues or pull requests.
jgl-template/GLGameTemplate/Game.h
2013-01-05 12:41:46 +01:00

67 lines
1.9 KiB
C++

//
// Game.h
// SkyRoads
//
// Created by Joshua Moerman on 12/22/12.
// Copyright (c) 2012 Vadovas. All rights reserved.
//
#ifndef SkyRoads_Game_h
#define SkyRoads_Game_h
#include "myGL.h"
constexpr static const char* attributes[] = {
"position",
"normal",
"color",
"tex_coord0"
};
template <size_t N>
std::vector<std::string> from_strarray(const char* const (& strs)[N]) {
std::vector<std::string> v(N);
for(auto i = 0; i < N; ++i){
v.push_back(strs[i]);
}
return v;
}
inline std::string getPath(std::string name, std::string kind) {
return [[[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:name.c_str()] ofType:[NSString stringWithUTF8String:kind.c_str()]] UTF8String];
}
struct Game{
std::vector<gl::Vertex> triangle{
{10.0, 0.0, 3.0, 1.0, 1.0, 1.0, 1.0},
{-8.0, 5.0, -3.0, 1.0, 1.0, 1.0, 1.0},
{-8.0, -5.0, 0.0, 1.0, 0.0, 1.0, 1.0}
};
J::shader basic_shader;
Game()
: basic_shader(getPath("basic_shader", "vsh"), getPath("basic_shader", "fsh"), from_strarray(attributes))
{}
void update(float dt){
}
void draw(){
GLKMatrix4 projectionMatrix = GLKMatrix4MakeOrtho(0.0, 768.0, 1024.0, 0.0f, -100, 100.0);
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(50.0f, 50.0f, 0.0f);
GLKMatrix4 modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix);
GLKMatrix3 normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(modelViewMatrix), NULL);
basic_shader.begin();
basic_shader.set_uniform("modelViewProjectionMatrix", modelViewProjectionMatrix.m, 0);
basic_shader.set_uniform("normalMatrix", normalMatrix.m, 0);
basic_shader.set_attribute("position", 3, GL_FLOAT, GL_FALSE, sizeof(gl::Vertex), &triangle[0][0]);
basic_shader.set_attribute("color", 4, GL_FLOAT, GL_FALSE, sizeof(gl::Vertex), &triangle[0][3]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, triangle.size());
basic_shader.end();
}
};
#endif