Browse Source

initial commit

master
Marrub 6 years ago
commit
afe2aa5277
6 changed files with 3247 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +49
    -0
      CMakeLists.txt
  3. +41
    -0
      FindSDL2.cmake
  4. +1271
    -0
      gl_core_3_3.c
  5. +1683
    -0
      gl_core_3_3.h
  6. +201
    -0
      main.c

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
*.kdev*
build/*

+ 49
- 0
CMakeLists.txt View File

@@ -0,0 +1,49 @@
cmake_minimum_required(VERSION 2.4)

if(COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
endif()

set(PROJ_NAME fxt)
set(SRC .)

project(${PROJ_NAME})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMake")

find_package(SDL2 REQUIRED)
find_package(OpenGL REQUIRED)

if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MT /Oy /Oi /W3 /FAcs /GF /fp:fast /wd4996 /D_CRT_SECURE_NO_DEPRECATE")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MT /Oy /Oi /W3 /FAcs /GF /fp:fast /wd4996 /D_CRT_SECURE_NO_DEPRECATE")
add_definitions(-DHAVE_FAST_MATH)
endif()

if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffast-math")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffast-math")
set(MATHLIB m)
endif()

if(MINGW)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libstdc++ -static-libgcc")
endif()

include_directories(
${SRC}
${SDL2_INCLUDE_DIRS}
${OPENGL_INCLUDE_DIR}
)

add_executable(${PROJ_NAME} WIN32
${SRC}/main.c
${SRC}/gl_core_3_3.c
)

target_link_libraries(${PROJ_NAME}
${SDL2_LIBRARIES}
${MATHLIB}
${OPENGL_LIBRARIES}
)

+ 41
- 0
FindSDL2.cmake View File

@@ -0,0 +1,41 @@
SET(SDL2_SEARCH_PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw
/opt/local
/opt/csw
/opt
)

FIND_PATH(SDL2_INCLUDE_DIRS SDL.h
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES include/SDL2 include
PATHS ${SDL2_SEARCH_PATHS}
)

FIND_LIBRARY(SDL2_LIBRARIES
NAMES SDL2
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES lib64 lib
PATHS ${SDL2_SEARCH_PATHS}
)

FIND_LIBRARY(SDL2MAIN_LIBRARIES
NAMES SDL2main
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES lib64 lib
PATHS ${SDL2_SEARCH_PATHS}
)

INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARIES SDL2_INCLUDE_DIRS)

SET(SDL2_FOUND "NO")
IF(SDL2_LIBRARIES AND SDL2_INCLUDE_DIRS)
SET(SDL2_FOUND "YES")
ENDIF(SDL2_LIBRARIES AND SDL2_INCLUDE_DIRS)

+ 1271
- 0
gl_core_3_3.c
File diff suppressed because it is too large
View File


+ 1683
- 0
gl_core_3_3.h
File diff suppressed because it is too large
View File


+ 201
- 0
main.c View File

@@ -0,0 +1,201 @@
#include "gl_core_3_3.h"
#include "SDL.h"
#include "SDL_opengl.h"
#include <stdio.h>
struct renderinfo
{
int ScrW;
int ScrH;
SDL_Window *Window;
SDL_Renderer *Renderer;
SDL_GLContext Context;
GLuint VertShader;
GLuint FragShader;
GLuint Program;
GLuint VBO;
GLuint EBO;
struct
{
GLint Position;
} Attrib;
struct
{
GLint Ticks;
} Uniform;
};
struct renderinfo re;
GLuint R_CreateBuffer(GLenum type, void const *data, GLsizei size)
{
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(type, buffer);
glBufferData(type, size, data, GL_STATIC_DRAW);
return buffer;
}
GLuint R_CreateShader(GLenum type, char const *src)
{
char log[512];
GLint success;
GLuint shader;
shader = glCreateShader(type);
glShaderSource(shader, 1, &src, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(shader, 512, NULL, log);
printf("Shader compile failed: %s\n", log);
exit(1);
}
return shader;
}
char const *FXT_ReadFile(char const *fname)
{
FILE *fp;
char *buf;
size_t size;
fp = fopen(fname, "r");
buf = malloc(4096);
size = fread(buf, 1, 4096, fp);
buf[size] = '\0';
return realloc(buf, size + 1);
}
void R_InitGL()
{
GLfloat const vertices[] = {
-1.0, -1.0,
1.0, -1.0,
-1.0, 1.0,
1.0, 1.0
};
GLushort const indices[] = { 0, 1, 2, 3 };
// Init viewport.
glViewport(0, 0, re.ScrW, re.ScrH);
glClearColor(0.2, 0.3, 0.3, 1.0);
// Init buffers.
re.VBO = R_CreateBuffer(GL_ARRAY_BUFFER, vertices, sizeof(vertices));
re.EBO = R_CreateBuffer(GL_ELEMENT_ARRAY_BUFFER, indices, sizeof(indices));
// Init shaders.
re.VertShader = R_CreateShader(GL_VERTEX_SHADER, FXT_ReadFile("main.vp.glsl"));
re.FragShader = R_CreateShader(GL_FRAGMENT_SHADER, FXT_ReadFile("main.fp.glsl"));
re.Program = glCreateProgram();
glAttachShader(re.Program, re.VertShader);
glAttachShader(re.Program, re.FragShader);
glLinkProgram(re.Program);
// Init uniform and attribute locations.
re.Uniform.Ticks = glGetUniformLocation(re.Program, "Ticks");
re.Attrib.Position = glGetAttribLocation(re.Program, "Position");
}
void R_Quit()
{
// Delete GL bound stuff.
glDeleteShader(re.VertShader);
glDeleteShader(re.FragShader);
glDeleteProgram(re.Program);
glDeleteBuffers(1, &re.VBO);
// Delete SDL bound stuff.
SDL_DestroyWindow(re.Window);
SDL_Quit();
}
void R_Init()
{
re.ScrW = 640;
re.ScrH = 480;
// Init GL info.
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
// Init window.
SDL_CreateWindowAndRenderer(re.ScrW, re.ScrH, SDL_WINDOW_SHOWN, &re.Window, &re.Renderer);
// Init GL context.
re.Context = SDL_GL_CreateContext(re.Window);
ogl_LoadFunctions();
SDL_GL_SetSwapInterval(1);
R_InitGL();
atexit(R_Quit);
}
void R_Main()
{
glClear(GL_COLOR_BUFFER_BIT);
// Bind shader.
glUseProgram(re.Program);
// Send uniforms.
glUniform1i(re.Uniform.Ticks, SDL_GetTicks());
// Bind vertex buffer.
glBindBuffer(GL_ARRAY_BUFFER, re.VBO);
glVertexAttribPointer(re.Attrib.Position, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, NULL);
glEnableVertexAttribArray(re.Attrib.Position);
// Bind element buffer, draw.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, re.EBO);
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, NULL);
// Disable vertex buffer.
glDisableVertexAttribArray(re.Attrib.Position);
// Swap video buffer (send to screen).
SDL_GL_SwapWindow(re.Window);
}
#ifdef _WIN32
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow)
#else
int main(int argc, char **argv)
#endif
{
SDL_Event ev;
SDL_Init(SDL_INIT_EVERYTHING);
R_Init();
for(;;)
{
while(SDL_PollEvent(&ev) != 0)
{
if(ev.type == SDL_QUIT)
{
exit(0);
}
}
R_Main();
}
return 0;
}

Loading…
Cancel
Save