This commit is contained in:
Frank DeMarco 2018-11-21 23:25:36 -05:00
commit 7e6dc58374
11 changed files with 531 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
examples/
*.o
Main.app/
main
glm/
sdl2-gfx/

BIN
Field.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

70
Makefile Normal file
View File

@ -0,0 +1,70 @@
SDLCONFIG = /home/frank/local/sdl/bin/sdl2-config
CFLAGS = $(shell $(SDLCONFIG) --cflags) -Wall -O2
LFLAGS = $(shell $(SDLCONFIG) --libs)
export ANDROID_HOME = /home/frank/ext/software/android-sdk
export ANDROID_NDK_HOME = /home/frank/ext/software/android-ndk-r8d
BUILDDIR = build
ANDROIDPROJECT = com.tarecontrol.demo
SDLHOME = /home/frank/ext/software/SDL2-2.0.8
PROJECTHOME = /home/frank/projects/public/games/pp/sdl
EMSCRIPTENHOME = /home/frank/ext/software/emsdk/emscripten/tag-1.38.12
SDLEMLIBSHOME = $(SDLHOME)/build/em/build/.libs
EMBUILDDIR = em
WINBUILDDIR = win
SDLMINGWHOME = $(SDLHOME)/i686-w64-mingw32
APPDIR = Main.app/Contents
SYSFWPATH = /Library/Frameworks
INC = -Iglm -Isdl2-gfx
linux :
g++ -c $(CFLAGS) $(INC) -D__LINUX__ main.cpp sdl2-gfx/SDL2_rotozoom.c
g++ $(LFLAGS) SDL2_rotozoom.o main.o -lSDL2_image -lGL -o main
android :
if [ ! -d $(BUILDDIR) ]; then mkdir $(BUILDDIR); fi;
cd $(SDLHOME)/build-scripts/ && \
./androidbuild.sh $(ANDROIDPROJECT) $(PROJECTHOME)/main.cpp
cp -r $(SDLHOME)/build/$(ANDROIDPROJECT) $(BUILDDIR)
sed -i s/2\.3\.3/2\.2\.3/g $(BUILDDIR)/$(ANDROIDPROJECT)/build.gradle
sed -i s/26\.0\.1/23\.0\.1/g $(BUILDDIR)/$(ANDROIDPROJECT)/app/build.gradle
cd $(BUILDDIR)/$(ANDROIDPROJECT) && ./gradlew assembleDebug
emscripten :
if [ ! -d $(BUILDDIR)/$(EMBUILDDIR) ]; then mkdir -p $(BUILDDIR)/$(EMBUILDDIR); fi;
cd $(BUILDDIR)/$(EMBUILDDIR) && \
$(EMSCRIPTENHOME)/em++ -O2 $(PROJECTHOME)/main.cpp -I$(SDLHOME)/include \
-Wall -s USE_SDL=2 -o main.html
mingw :
if [ ! -d $(BUILDDIR)/$(WINBUILDDIR) ]; then mkdir -p $(BUILDDIR)/$(WINBUILDDIR); fi;
cd $(BUILDDIR)/$(WINBUILDDIR) && \
i686-w64-mingw32-g++ $(PROJECTHOME)/main.cpp -I$(SDLMINGWHOME)/include/SDL2 -L$(SDLMINGWHOME)/lib \
-Wl,-rpath,$(SDLMINGWHOME)/lib -lmingw32 -lSDL2main -lSDL2 -lopengl32 -Wall -O2 -o main.exe && \
cp $(SDLMINGWHOME)/bin/SDL2.dll .
osx :
g++ -I $(SYSFWPATH)/SDL2.framework/Headers $(INC) \
-I $(SYSFWPATH)/SDL2_image.framework/Headers -Wl,-rpath,$(SYSFWPATH) \
-framework SDL2 -framework SDL2_image -framework OpenGL main.cpp sdl2-gfx/SDL2_rotozoom.c \
-o main
osx-bundle :
if [ ! -d "$(APPDIR)" ]; then mkdir -p $(APPDIR); fi;
if [ ! -d "$(APPDIR)" ]; then mkdir $(APPDIR); fi;
if [ ! -d "$(APPDIR)/MacOS" ]; then mkdir $(APPDIR)/MacOS; fi;
if [ ! -d "$(APPDIR)/Frameworks" ]; then mkdir $(APPDIR)/Frameworks; fi;
if [ ! -d "$(APPDIR)/Resources" ]; then mkdir $(APPDIR)/Resources; fi;
touch $(APPDIR)/Info.plist
cp -r $(SYSFWPATH)/SDL2.framework $(APPDIR)/Frameworks
cp -r $(SYSFWPATH)/SDL2_image.framework $(APPDIR)/Frameworks
g++ -I $(SYSFWPATH)/SDL2.framework/Headers -I $(SYSFWPATH)/SDL2_image.framework/Headers \
-Wl,-rpath,@executable_path/../Frameworks -Wl,-rpath,$(SYSFWPATH) \
-framework SDL2 -framework SDL2_image -framework OpenGL main.cpp -o $(APPDIR)/MacOS/main
cross : linux android emscripten mingw
# main : main.o
# g++ -o main $(LFLAGS) -lGL main.o
# main.o : main.cpp
# g++ -c $(CFLAGS) -DLINUX main.cpp

9
WEBRESOURCES Normal file
View File

@ -0,0 +1,9 @@
https://glm.g-truc.net/0.9.9/api/index.html
http://www.ferzkopp.net/Software/SDL2_gfx/Docs/html/index.html
http://ogldev.atspace.co.uk/
https://www.khronos.org/registry/OpenGL-Refpages/gl4/
https://www.khronos.org/opengl/wiki/Rendering_Pipeline_Overview
http://www.opengl-tutorial.org/
http://www.songho.ca/opengl/index.html
http://www.learnopengles.com/
http://wiki.libsdl.org/

BIN
background.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

1
log Normal file
View File

@ -0,0 +1 @@
Error test

400
main.cpp Normal file
View File

@ -0,0 +1,400 @@
#include <stdio.h>
#include <math.h>
#include <SDL.h>
#include <SDL_image.h>
#if defined(__LINUX__) || defined(__MINGW32__)
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#elif defined(__ANDROID__)
#include <GLES/gl.h>
#elif defined(__EMSCRIPTEN__)
#include <GLES2/gl2.h>
#elif defined(__APPLE__)
#include <OpenGL/gl3.h>
#endif
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/string_cast.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <SDL2_rotozoom.h>
char* file_to_buf(const char *path)
{
FILE *fp;
long length;
char *buf;
fp = fopen(path, "rb");
if (!fp)
{
return NULL;
}
fseek(fp, 0, SEEK_END);
length = ftell(fp);
buf = (char*) malloc(length + 1);
fseek(fp, 0, SEEK_SET);
fread(buf, length, 1, fp);
fclose(fp);
buf[length] = 0;
return buf;
}
int main(int argc, char *argv[])
{
SDL_version version;
SDL_GetVersion(&version);
printf("SDL %d.%d.%d\n", version.major, version.minor, version.patch);
fprintf(stderr, "Error test\n");
SDL_SetMainReady();
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
fprintf(stderr, "SDL could not initalize! SDL_Error: %s\n", SDL_GetError());
}
else
{
int sw = 640, sh = 480;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_Window *window = SDL_CreateWindow(
"TARE control",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
sw,
sh,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (window == NULL)
{
fprintf(stderr, "Could not create window: %s\n", SDL_GetError());
return 1;
}
else
{
SDL_ShowCursor(0);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer == NULL)
{
fprintf(stderr, "Could not create renderer! SDL_Error: %s\n", SDL_GetError());
}
else
{
SDL_Texture *texture = IMG_LoadTexture(renderer, "background.png");
if (!texture)
{
fprintf(stderr, "Could not load image! SDL_Error: %s\n", SDL_GetError());
}
else
{
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
printf("OpenGL %s\n", glGetString(GL_VERSION));
SDL_Event event;
int done = 0, x = 0;
float r = 0.0;
int i;
// Create handles for our Vertex Array Object and two Vertex Buffer Objects
GLuint vao, vbo;
int IsCompiled_VS, IsCompiled_FS;
int IsLinked;
int maxLength;
char *vertexInfoLog;
char *fragmentInfoLog;
char *shaderProgramInfoLog;
// We're going to create a simple diamond made from lines
GLfloat diamond[4][2] = {
{ 0.0, 1.0 },
{ 1.0, 0.0 },
{ 0.0, -1.0 },
{ -1.0, 0.0 } };
const GLfloat diamond_colors[4][3] = {
{ 1.0, 0.0, 0.0 },
{ 0.0, 1.0, 0.0 },
{ 0.0, 0.0, 1.0 },
{ 1.0, 1.0, 1.0 } };
GLfloat cube[36][3] = {
{1, 1, 1}, {-1, 1, 1}, {-1,-1, 1}, // v0-v1-v2 (front)
{-1,-1, 1}, {1,-1, 1}, {1, 1, 1}, // v2-v3-v0
{1, 1, 1}, {1,-1, 1}, {1,-1,-1}, // v0-v3-v4 (right)
{1,-1,-1}, {1, 1,-1}, {1, 1, 1}, // v4-v5-v0
{1, 1, 1}, {1, 1,-1}, {-1, 1,-1}, // v0-v5-v6 (top)
{-1, 1,-1}, {-1, 1, 1}, {1, 1, 1}, // v6-v1-v0
{-1, 1, 1}, {-1, 1,-1}, {-1,-1,-1}, // v1-v6-v7 (left)
{-1,-1,-1}, {-1,-1, 1}, {-1, 1, 1}, // v7-v2-v1
{-1,-1,-1}, {1,-1,-1}, {1,-1, 1}, // v7-v4-v3 (bottom)
{1,-1, 1}, {-1,-1, 1}, {-1,-1,-1}, // v3-v2-v7
{1,-1,-1}, {-1,-1,-1}, {-1, 1,-1}, // v4-v7-v6 (back)
{-1, 1,-1}, {1, 1,-1}, {1,-1,-1} }; // v6-v5-v4
GLfloat cube_colors[36][3] = {
{1, 0, 0}, {1, 0, 0}, {1, 0, 0}, // v0-v1-v2 (front)
{1, 0, 0}, {1, 0, 0}, {1, 0, 0}, // v2-v3-v0
{0, 1, 0}, {0, 1, 0}, {0, 1, 0}, // v0-v3-v4 (right)
{0, 1, 0}, {0, 1, 0}, {0, 1, 0}, // v4-v5-v0
{0, 0, 1}, {0, 0, 1}, {0, 0, 1}, // v0-v5-v6 (top)
{0, 0, 1}, {0, 0, 1}, {0, 0, 1}, // v6-v1-v0
{1, 1, 0}, {1, 1, 0}, {1, 1, 0}, // v1-v6-v7 (left)
{1, 1, 0}, {1, 1, 0}, {1, 1, 0}, // v7-v2-v1
{0, 1, 1}, {0, 1, 1}, {0, 1, 1}, // v7-v4-v3 (bottom)
{0, 1, 1}, {0, 1, 1}, {0, 1, 1}, // v3-v2-v7
{1, 0, 1}, {1, 0, 1}, {1, 0, 1}, // v4-v7-v6 (back)
{1, 0, 1}, {1, 0, 1}, {1, 0, 1} }; // v6-v5-v4
glm::mat4 projection = glm::perspective(glm::radians(45.0f),
(float) sw / (float) sh, 0.1f, 100.0f);
glm::mat4 view = glm::lookAt(
glm::vec3(4, 3, 3),
glm::vec3(0, 0, 0),
glm::vec3(0, 1, 0));
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 mvp;
// These pointers will receive the contents of our shader source code files
GLchar *vertexsource, *fragmentsource;
// These are handles used to reference the shaders
GLuint vertexshader, fragmentshader;
// This is a handle to the shader program
GLuint shaderprogram;
// int q = SDL_GL_BindTexture(texture, NULL, NULL);
// printf("bind texture returns %i\n", q);
SDL_Surface *surface = rotateSurface90Degrees(
IMG_Load("tile.png"), 2);
printf("bytes per pixel %i\n", surface->format->BytesPerPixel);
GLuint t_id;
glBindTexture(GL_TEXTURE_2D, t_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_BGRA,
GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
GLfloat cube_uv[72] = {
1, 1, 0, 1, 0, 0,
0, 0, 1, 0, 1, 1,
0, 1, 0, 0, 1, 0,
1, 0, 1, 1, 0, 1,
0, 1, 0, 0, 1, 0,
1, 0, 1, 1, 0, 1,
1, 1, 0, 1, 0, 0,
0, 0, 1, 0, 1, 1,
0, 0, 1, 0, 1, 1,
1, 1, 0, 1, 0, 0,
0, 0, 1, 0, 1, 1,
1, 1, 0, 1, 0, 0};
GLuint uvbuffer;
glGenBuffers(1, &uvbuffer);
// Allocate and assign a Vertex Array Object to our handle
glGenVertexArrays(1, &vao);
// Bind our Vertex Array Object as the current used object
glBindVertexArray(vao);
// Allocate and assign two Vertex Buffer Objects to our handle
glGenBuffers(1, &vbo);
// Bind our first VBO as being the active buffer and storing vertex coordinates
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// Copy the vertex data from diamond to our buffer
// 8 * sizeof(GLfloat) is the size of the diamond array
glBufferData(GL_ARRAY_BUFFER, 108 * sizeof(GLfloat), cube,
GL_STATIC_DRAW);
// Specify that our coordinate data is going into attribute
// index 0, and contains two floats per vertex
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Enable attribute index 0 as being used
glEnableVertexAttribArray(0);
// Bind our second VBO as being the active buffer and storing vertex colors
// glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
// Copy the color data from colors to our buffer
// 12 * sizeof(GLfloat) is the size of the colors array
// glBufferData(GL_ARRAY_BUFFER, 108 * sizeof(GLfloat),
// cube_colors, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_uv), cube_uv, GL_STATIC_DRAW);
// Specify that our color data is going into attribute index 1
// and contains three floats per vertex
// glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
// Enable attribute index 1
glEnableVertexAttribArray(1);
// Read our shaders into the appropriate buffers
vertexsource = file_to_buf("triangle.vert");
fragmentsource = file_to_buf("triangle.frag");
// Create an empty vertex shader handle
vertexshader = glCreateShader(GL_VERTEX_SHADER);
// Send the vertex shader source code to GL
// Note that the source code is NULL character terminated
// GL will automatically detect that therefore the length
// info can be 0 in this case (the last parameter)
glShaderSource(vertexshader, 1, (const GLchar**) &vertexsource, 0);
// Compile the vertex shader
glCompileShader(vertexshader);
glGetShaderiv(vertexshader, GL_COMPILE_STATUS, &IsCompiled_VS);
if(IsCompiled_VS == 0)
{
glGetShaderiv(vertexshader, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
vertexInfoLog = (char *) malloc(maxLength);
glGetShaderInfoLog(vertexshader, maxLength, &maxLength, vertexInfoLog);
// Handle the error in an appropriate way
// such as displaying a message or writing to a log file.
fprintf(stderr, "vertex shader compilation failed %s\n", vertexInfoLog);
free(vertexInfoLog);
return 0;
}
// Create an empty fragment shader handle
fragmentshader = glCreateShader(GL_FRAGMENT_SHADER);
// Send the fragment shader source code to GL
// Note that the source code is NULL character terminated
// GL will automatically detect that therefore the length info
// can be 0 in this case (the last parameter)
glShaderSource(fragmentshader, 1, (const GLchar**) &fragmentsource, 0);
// Compile the fragment shader
glCompileShader(fragmentshader);
glGetShaderiv(fragmentshader, GL_COMPILE_STATUS, &IsCompiled_FS);
if(IsCompiled_FS == 0)
{
glGetShaderiv(fragmentshader, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
fragmentInfoLog = (char *)malloc(maxLength);
glGetShaderInfoLog(fragmentshader, maxLength, &maxLength, fragmentInfoLog);
// Handle the error in an appropriate way such as displaying a
// message or writing to a log file
fprintf(stderr, "fragment shader compilation failed %s\n", fragmentInfoLog);
free(fragmentInfoLog);
return 0;
}
// If we reached this point it means the vertex and fragment
// shaders compiled and are syntax error free
// We must link them together to make a GL shader program
// GL shader programs are monolithic. It is a single piece made of
// 1 vertex shader and 1 fragment shader
// Assign our program handle a "name"
shaderprogram = glCreateProgram();
// Attach our shaders to our program
glAttachShader(shaderprogram, vertexshader);
glAttachShader(shaderprogram, fragmentshader);
// Bind attribute index 0 (coordinates) to in_Position and attribute
// index 1 (color) to in_Color
// Attribute locations must be setup before calling glLinkProgram
glBindAttribLocation(shaderprogram, 0, "in_Position");
// glBindAttribLocation(shaderprogram, 1, "in_Color");
glBindAttribLocation(shaderprogram, 1, "vertexUV");
// Link our program
// At this stage, the vertex and fragment programs are inspected,
// optimized and a binary code is generated for the shader
// The binary code is uploaded to the GPU, if there is no error
glLinkProgram(shaderprogram);
GLuint m_id = glGetUniformLocation(shaderprogram, "MVP");
GLuint t_uniform_id = glGetUniformLocation(shaderprogram, "myTextureSampler");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, t_id);
glUniform1i(t_uniform_id, 0);
// Again, we must check and make sure that it linked
// If it fails, it would mean either there is a mismatch between the vertex
// and fragment shaders. It might be that you have surpassed your
// GPU's abilities. Perhaps too many ALU operations or
// too many texel fetch instructions or too many interpolators or
// dynamic loops
glGetProgramiv(shaderprogram, GL_LINK_STATUS, (int *) &IsLinked);
if(IsLinked == 0)
{
// Noticed that glGetProgramiv is used to get the length for
// a shader program, not glGetShaderiv
glGetProgramiv(shaderprogram, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
shaderProgramInfoLog = (char *) malloc(maxLength);
// Notice that glGetProgramInfoLog, not glGetShaderInfoLog
glGetProgramInfoLog(shaderprogram, maxLength, &maxLength,
shaderProgramInfoLog);
// Handle the error in an appropriate way such as displaying a
// message or writing to a log file
fprintf(stderr, "shader linking failed %s\n", shaderProgramInfoLog);
free(shaderProgramInfoLog);
return 0;
}
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
// Load the shader into the rendering pipeline
glUseProgram(shaderprogram);
while (!done)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
done = 1;
}
}
// glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
// glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), diamond, GL_STATIC_DRAW);
// glClearColor((sin(r) + 1) / 2, (sin(r) + 1) / 2, .5, 1);
glClearColor(.7, .7, .5, 1);
r += .025;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
model = glm::rotate(model, .01f, glm::vec3(0.0f, 1.0f, 0.0f));
mvp = projection * view * model;
glUniformMatrix4fv(m_id, 1, GL_FALSE, &mvp[0][0]);
glDrawArrays(GL_TRIANGLES, 0, 36);
//glFlush();
//SDL_Rect rect = {x++, 0, 240, 160};
//SDL_RenderCopy(renderer, texture, NULL, &rect);
//SDL_RenderPresent(renderer);
SDL_GL_SwapWindow(window);
SDL_Delay(40);
}
SDL_GL_DeleteContext(glcontext);
SDL_DestroyTexture(texture);
}
SDL_DestroyRenderer(renderer);
}
SDL_DestroyWindow(window);
}
}
SDL_Quit();
return 0;
}

1
os-notes Normal file
View File

@ -0,0 +1 @@
download dmg of sdl2 sdl2_image to /Library/Frameworks

BIN
tile.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 703 B

16
triangle.frag Normal file
View File

@ -0,0 +1,16 @@
#version 130
// It was expressed that some drivers required this next line to function properly
precision highp float;
// in vec3 ex_Color;
in vec2 UV;
// out vec4 FragColor;
out vec4 color;
uniform sampler2D myTextureSampler;
void main(void) {
// Pass through our original color with full opacity.
// FragColor = vec4(ex_Color,1.0);
color = texture(myTextureSampler, UV);
}

28
triangle.vert Normal file
View File

@ -0,0 +1,28 @@
#version 130
// in_Position was bound to attribute index 0 and in_Color was bound to attribute index 1
in vec3 in_Position;
// in vec3 in_Color;
in vec2 vertexUV;
// We output the ex_Color variable to the next shader in the chain
// out vec3 ex_Color;
out vec2 UV;
uniform mat4 MVP;
// uniform mat4 MVP = mat4(vec4(1, 0, 0, 0), vec4(0, 1, 0, 0), vec4(0, 0, 1, 0), vec4(0, 0, 0, 1));
void main(void) {
// Since we are using flat lines, our input only had two points: x and y.
// Set the Z coordinate to 0 and W coordinate to 1
// gl_Position = vec4(in_Position.x / 2, in_Position.y / 2, in_Position.z / 2, 1.0);
gl_Position = MVP * vec4(in_Position, 1);
// GLSL allows shorthand use of vectors too, the following is also valid:
// gl_Position = vec4(in_Position, 1.0);
// We're simply passing the color through unmodified
// ex_Color = in_Color;
UV = vertexUV;
}