Can draw a triangle

This commit is contained in:
Morten Delenk 2016-08-27 21:59:52 +02:00
parent e97b3a24c4
commit 4d5b548ffe
No known key found for this signature in database
GPG key ID: 3F818D0F65DCB490
6 changed files with 229 additions and 4 deletions

46
.kdev4/testgame.kdev4 Normal file
View file

@ -0,0 +1,46 @@
[CMake]
Build Directory Count=1
Current Build Directory Index=0
ProjectRootRelative=./
[CMake][CMake Build Directory 0]
Build Directory Path=file:///usr/home/morten/projects/testgame/build
Build Type=Debug
CMake Binary=file:///usr/local/bin/cmake
Environment Profile=
Extra Arguments=
Install Directory=
[CustomDefinesAndIncludes][ProjectPath0]
Defines=\x00\x00\x00\x00
Includes=\x00\x00\x00\x01\x00\x00\x00H\x00/\x00u\x00s\x00r\x00/\x00h\x00o\x00m\x00e\x00/\x00m\x00o\x00r\x00t\x00e\x00n\x00/\x00p\x00r\x00o\x00j\x00e\x00c\x00t\x00s\x00/\x00t\x00e\x00s\x00t\x00g\x00a\x00m\x00e\x00/\x00.
Path=.
[Defines And Includes][Compiler]
Name=GCC
Path=gcc
Type=GCC
[Launch]
Launch Configurations=Launch Configuration 0
[Launch][Launch Configuration 0]
Configured Launch Modes=execute
Configured Launchers=nativeAppLauncher
Name=testgame
Type=Native Application
[Launch][Launch Configuration 0][Data]
Arguments=
Dependencies=@Variant(\x00\x00\x00\t\x00\x00\x00\x00\x01\x00\x00\x00\x0b\x00\x00\x00\x00\x02\x00\x00\x00\x10\x00t\x00e\x00s\x00t\x00g\x00a\x00m\x00e\x00\x00\x00\x10\x00t\x00e\x00s\x00t\x00g\x00a\x00m\x00e)
Dependency Action=Build
EnvironmentGroup=
Executable=file:///usr/home/morten/projects/testgame/
External Terminal=konsole --noclose --workdir %workdir -e %exe
Project Target=testgame,testgame
Use External Terminal=false
Working Directory=
isExecutable=false
[Project]
VersionControlSupport=kdevgit

View file

@ -1,6 +1,13 @@
cmake_minimum_required(VERSION 2.6)
project(testgame)
add_executable(testgame main.cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -I..")
set(CMAKE_LD_FLAGS "${CMAKE_LD_FLAGS} -L/usr/local/bin")
add_executable(testgame main.cpp renderer/main.cpp)
INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS})
find_library(SDL SDL2)
find_library(GLEW GLEW)
find_library(GL GL)
TARGET_LINK_LIBRARIES(testgame ${SDL} ${GLEW} ${GL})
install(TARGETS testgame RUNTIME DESTINATION bin)

112
main.cpp
View file

@ -1,6 +1,116 @@
#include <iostream>
#include <cstdlib>
#include <main.h>
#include <SDL.h>
#include <GL/glew.h>
using namespace std;
bool TestGame::init()
{
GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;
//Compile Vertex shader
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
const char *vs_source =
//"#version 100\n" // OpenGL ES 2.0
"#version 120\n" // OpenGL 2.1
"attribute vec2 coord2d; "
"void main(void) { "
" gl_Position = vec4(coord2d, 0.0, 1.0); "
"}";
glShaderSource(vs, 1, &vs_source, NULL);
glCompileShader(vs);
glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_ok);
if (!compile_ok) {
cerr << "Error in vertex shader" << endl;
return false;
}
//Compile Fragment shader
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
const char *fs_source =
//"#version 100\n" // OpenGL ES 2.0
"#version 120\n" // OpenGL 2.1
"void main(void) { "
" gl_FragColor[0] = 0.0; "
" gl_FragColor[1] = 0.0; "
" gl_FragColor[2] = 1.0; "
"}";
glShaderSource(fs, 1, &fs_source, NULL);
glCompileShader(fs);
glGetShaderiv(fs, GL_COMPILE_STATUS, &compile_ok);
if (!compile_ok) {
cerr << "Error in fragment shader" << endl;
return false;
}
//Linking
program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
if (!link_ok) {
cerr << "Error in glLinkProgram" << endl;
return false;
}
const char* attribute_name = "coord2d";
attribute_coord2d = glGetAttribLocation(program, attribute_name);
if (attribute_coord2d == -1) {
cerr << "Could not bind attribute " << attribute_name << endl;
return false;
}
return true;
}
void TestGame::stop()
{
//To be written
}
bool TestGame::tick()
{
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
if (ev.type == SDL_QUIT)
return false;
}
render();
return true;
}
void TestGame::render()
{
//Clear Background
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glEnableVertexAttribArray(attribute_coord2d);
GLfloat triangle_vertices[] = {
0.0, 0.8,
-0.8, -0.8,
0.8, -0.8,
};
// Describe our vertices array to OpenGL (it can't guess its format automatically)
glVertexAttribPointer(
attribute_coord2d, // attribute
2, // number of elements per vertex, here (x,y)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
triangle_vertices // pointer to the C array
);
// Push each element in buffer_vertices to the vertex shader
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(attribute_coord2d);
// Display the result
SDL_GL_SwapWindow(window);
}
TestGame::~TestGame()
{
glDeleteProgram(program);
}
int main(int argc, char **argv) {
std::cout << "Hello, world!" << std::endl;
TestGame game;
game.start();
return 0;
}

15
main.h Normal file
View file

@ -0,0 +1,15 @@
#include <renderer/main.h>
#include <GL/glew.h>
class TestGame: public MainClass {
private:
GLuint program;
GLint attribute_coord2d;
auto render() -> void;
protected:
virtual auto tick() -> bool;
virtual auto init() -> bool;
public:
TestGame(): MainClass(640,480) {};
virtual ~TestGame();
virtual auto stop() -> void;
};

30
renderer/main.cpp Normal file
View file

@ -0,0 +1,30 @@
#include <renderer/main.h>
#include <iostream>
#include <GL/glew.h>
using namespace std;
MainClass::MainClass(int width, int height)
{
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Testgame", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
SDL_GL_CreateContext(window);
}
void MainClass::start()
{
/* Extension wrangler initialising */
GLenum glew_status = glewInit();
if (glew_status != GLEW_OK) {
cerr << "Error: glewInit: " << glewGetErrorString(glew_status) << endl;
throw nullptr;
}
if(!init()) {
cerr << "Error: could not init game!" << endl;
throw nullptr;
}
while(tick());
stop();
}
MainClass::~MainClass()
{
SDL_DestroyWindow(window);
SDL_Quit();
}

17
renderer/main.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef __RENDERER_MAIN_H
#define __RENDERER_MAIN_H
#include <SDL.h>
class MainClass {
private:
MainClass() {};
protected:
SDL_Window* window;
virtual auto tick() -> bool = 0;
virtual auto init() -> bool = 0;
public:
MainClass(int width, int height);
virtual ~MainClass();
virtual auto start() -> void;
virtual auto stop() -> void =0;
};
#endif