// Headerphile.com OpenGL Tutorial part 1
// A Hello World in the world of OpenGL ( creating a simple windonw and setting background color )
// Source code is an C++ adaption / simplicication of : https://www.opengl.org/wiki/Tutorial1:_Creating_a_Cross_Platform_OpenGL_3.2_Context_in_SDL_(C_/_SDL)
// Compile : clang++ main.cpp -lGL -lSDL2 -std=c++11 -o Test
// C++ Headers
//WINDOW in sdl2 with opengl write in c4droid on tablet lenovo
//to compile use sdl2 parametrs
//
#include <string>
#include <iostream>
// OpenGL / glew Headers
#define GL3_PROTOTYPES 1
//#include <GL/glew.h>
#include <iostream>
// SDL2 Headers
#include <SDL2/SDL.h>
#include <EGL/egl.h>
#include <GLES/gl.h>
#include <android/sensor.h>
#include <android/log.h>
#include <android_native_app_glue.h>
#define __glPi 3.14159265358979323846
//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
// Our SDL_Window ( just like with SDL2 wihout OpenGL)
SDL_Window *mainWindow;
// Our opengl context handle
SDL_GLContext mainContext;
namespace gl {
void glutMainLoop(void)
{
for (;;);
}
void draw()
{
// Cover with blue and update
glClearColor(0.0, 0.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(mainWindow);
}
bool initGL()
{
//Initialize Projection Matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
//Initialize Modelview Matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
//Initialize clear color
glClearColor( 0.f, 0.f, 0.f, 1.f );
//Check for error
GLenum error = glGetError();
if( error != GL_NO_ERROR )
{
return false;
}
return true;
}
};
using namespace gl;
std::string programName = "Headerphile SDL2 - OpenGL thing";
namespace SDL {
void CheckSDLError(int line);
bool SetOpenGLAttributes()
{
// Set our OpenGL version.
// SDL_GL_CONTEXT_CORE gives us only the newer version, deprecated functions are disabled
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
// 3.2 is part of the modern versions of OpenGL, but most video cards whould be able to run it
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
// Turn on double buffering with a 24bit Z buffer.
// You may need to change this to 16 or 32 for your system
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
return true;
}
bool Init()
{
// Initialize SDL's Video subsystem
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "Failed to init SDL\n";
return false;
}
// Create our window centered at 512x512 resolution
mainWindow = SDL_CreateWindow(programName.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL );
// Check that everything worked out okay
if (!mainWindow)
{
std::cout << "Unable to create window\n";
CheckSDLError(__LINE__);
return false;
}
// Create our opengl context and attach it to our window
mainContext = SDL_GL_CreateContext(mainWindow);
if( gl::initGL() == false )
{
return false;
}
SetOpenGLAttributes();
// This makes our buffer swap syncronized with the monitor's vertical refresh
SDL_GL_SetSwapInterval(1);
// Init GLEW
// Apparently, this is needed for Apple. Thanks to Ross Vander for letting me know
#ifndef __APPLE__
//glewExperimental = GL_TRUE;
//glewInit();
#endif
return true;
}
void Cleanup()
{
// Delete our OpengL context
SDL_GL_DeleteContext(mainContext);
// Destroy our window
SDL_DestroyWindow(mainWindow);
// Shutdown SDL 2
SDL_Quit();
}
void CheckSDLError(int line = -1)
{
std::string error = SDL_GetError();
if (error != "")
{
std::cout << "SLD Error : " << error << std::endl;
if (line != -1)
std::cout << "\nLine : " << line << std::endl;
SDL_ClearError();
}
}
void PrintSDL_GL_Attributes()
{
int value = 0;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &value);
std::cout << "SDL_GL_CONTEXT_MAJOR_VERSION : " << value << std::endl;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &value);
std::cout << "SDL_GL_CONTEXT_MINOR_VERSION: " << value << std::endl;
}
};
using namespace SDL;
int main(int argc, char *argv[])
{
if (!Init())
return -1;
draw();
glutMainLoop();
//RunGame();
Cleanup();
return 0;
}
No comments:
Post a Comment