// C++ Headers #include <string> #include <iostream> // OpenGL / glew Headers #define GL3_PROTOTYPES 1 //#include <GL/glew.h> // 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> //#include "SDL/SDL_opengl.h" //Screen attributes const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int SCREEN_BPP = 32; #include <GLES2/gl2.h> #include <SDL2/SDL.h> struct myData { SDL_Window *window; SDL_GLContext context; }; const GLchar vertex[] = "attribute vec4 position; \n" "void main() \n" "{ \n" " gl_Position = vec4(position.xyz, 1.0); \n" "} \n"; const GLchar fragment[] = "precision mediump float;\n" "void main() \n" "{ \n" " gl_FragColor = vec4 (0.0, 1.0, 1.0, 1.0 );\n" "} \n"; GLuint loadShader(GLuint program, GLenum type, const GLchar *shaderSrc) { GLuint shader; shader = glCreateShader(type); glShaderSource(shader, 1, &shaderSrc, NULL); glCompileShader(shader); glAttachShader(program, shader); return 0; } int sdlInit(myData *data) { SDL_Init(SDL_INIT_VIDEO); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); data->window = SDL_CreateWindow("Demo", 0, 0, 512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); data->context = SDL_GL_CreateContext(data->window); return 0; } int glInit(myData *data) { GLuint programObject; GLuint vbo; glGenBuffers(1, &vbo); GLfloat vertices[] = {0.0f, 0.9f, 0.9f, -0.9f, -0.9f, -0.9f}; glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); programObject = glCreateProgram(); loadShader(programObject, GL_VERTEX_SHADER, vertex); loadShader(programObject, GL_FRAGMENT_SHADER, fragment); glLinkProgram(programObject); glUseProgram(programObject); GLint posAttrib = glGetAttribLocation( programObject , "position"); glEnableVertexAttribArray(posAttrib); glVertexAttribPointer(posAttrib,2, GL_FLOAT, GL_FALSE, 0, 0); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glViewport(0, 0, 512, 512); return 0; } int loopFunc(myData *data) { SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { return 1; } } // Clear the screen to black glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // Draw a triangle from the 3 vertices glDrawArrays(GL_TRIANGLES, 0, 3); SDL_GL_SwapWindow(data->window); return 0; } void sdlQuit(myData *data) { SDL_GL_DeleteContext(data->context); SDL_DestroyWindow(data->window); SDL_Quit(); return; } int main() { myData data; sdlInit(&data); glInit(&data); while (!loopFunc(&data)); sdlQuit(&data); return 0; } Email This BlogThis! Share to Twitter Share to Facebook Share to Pinterest
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}