shiny
This commit is contained in:
parent
59dbec98a0
commit
6b231230db
4
Makefile
4
Makefile
|
@ -17,8 +17,8 @@ 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 -lSDL2_ttf -lGL -o main
|
||||
g++ -c $(CFLAGS) $(INC) -D__LINUX__ main.cpp sdl2-gfx/SDL2_gfxPrimitives.c sdl2-gfx/SDL2_rotozoom.c
|
||||
g++ $(LFLAGS) SDL2_gfxPrimitives.o SDL2_rotozoom.o main.o -lSDL2_image -lSDL2_ttf -lGL -o main
|
||||
|
||||
android :
|
||||
if [ ! -d $(BUILDDIR) ]; then mkdir $(BUILDDIR); fi;
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
#version 130
|
||||
|
||||
in vec4 ex_Color;
|
||||
in vec2 UV;
|
||||
uniform sampler2D myTextureSampler;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
gl_FragColor = texture(myTextureSampler, UV) * ex_Color;
|
||||
}
|
|
@ -6,10 +6,14 @@ in vec3 in_Color;
|
|||
|
||||
out vec2 UV;
|
||||
out vec4 ex_Color;
|
||||
out vec4 pos;
|
||||
|
||||
uniform float r;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
gl_Position = vec4(in_Position, 1);
|
||||
UV = vertexUV;
|
||||
ex_Color = vec4(in_Color, 1);
|
||||
ex_Color = vec4(r * in_Color[0], (1 - r) * in_Color[1], in_Color[2], 1);
|
||||
pos = vec4(0, 0, 0, 0);
|
||||
}
|
||||
|
|
200
main.cpp
200
main.cpp
|
@ -12,6 +12,7 @@
|
|||
#include <SDL.h>
|
||||
#include <SDL_image.h>
|
||||
#include <SDL_ttf.h>
|
||||
#include <SDL2_gfxPrimitives.h>
|
||||
|
||||
#if defined(__LINUX__) || defined(__MINGW32__)
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
|
@ -157,14 +158,14 @@ GLuint get_gl_texture_from_surface(SDL_Surface *surface, GLint mipmap_filter)
|
|||
|
||||
SDL_Surface* get_framerate_indicator_surface(int frame_count)
|
||||
{
|
||||
TTF_Font *font = TTF_OpenFont("SourceCodePro-Regular.otf", 14);
|
||||
TTF_Font *font = TTF_OpenFont("resource/SourceCodePro-Regular.otf", 14);
|
||||
SDL_Surface *shaded = TTF_RenderText_Shaded(
|
||||
font, std::to_string(frame_count).c_str(), {0, 0, 0}, {255, 255, 255});
|
||||
SDL_Surface *converted = SDL_ConvertSurfaceFormat(shaded, SDL_PIXELFORMAT_ARGB8888, 0);
|
||||
SDL_Surface *flipped = zoomSurface(converted, 1, -1, SMOOTHING_OFF);
|
||||
if (!flipped)
|
||||
{
|
||||
fprintf(stderr, "Could not create text! SDL_Error: %s\n", SDL_GetError());
|
||||
fprintf(stderr, "Could not create text %s\n", SDL_GetError());
|
||||
}
|
||||
return flipped;
|
||||
}
|
||||
|
@ -179,26 +180,41 @@ void set_framerate_indicator(int frame_count, GLuint id)
|
|||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
}
|
||||
|
||||
struct Sprite
|
||||
{
|
||||
std::list frames;
|
||||
|
||||
Sprite()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
struct Mushroom : Sprite
|
||||
{
|
||||
Mushroom() : Sprite()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
struct Game
|
||||
{
|
||||
SDL_Window *window;
|
||||
SDL_Renderer *renderer;
|
||||
SDL_GLContext glcontext;
|
||||
SDL_Renderer *renderer = NULL;
|
||||
SDL_GLContext glcontext = NULL;
|
||||
SDL_Texture *grass_texture;
|
||||
int sw = 640, sh = 480, framerate = 60, recording_capture_framerate = 100,
|
||||
frame_time_overflow = 0, capture_time_overflow = 0, frame_count = 0,
|
||||
last_frame_timestamp, frame_count_timestamp, last_capture_timestamp, ticks;
|
||||
bool done = false, is_recording = false, show_framerate = false;
|
||||
bool done = false, is_recording = false, show_framerate = false, is_gl_context = true;
|
||||
SDL_Event event;
|
||||
float r = 0.0;
|
||||
GLuint vbo, space_texture_id, mvp_id, framerate_texture_id, flat_program, world_program,
|
||||
fake_texture_id;
|
||||
float frame_length = 1000.0 / framerate;
|
||||
std::list<SDL_Surface*> frames;
|
||||
glm::mat4 projection, view, model, mvp;
|
||||
glm::mat4 projection, view, model = glm::mat4(1.0f), mvp;
|
||||
|
||||
Game() { }
|
||||
|
||||
bool initialize()
|
||||
Game()
|
||||
{
|
||||
setenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN", "0", true);
|
||||
SDL_version version;
|
||||
|
@ -208,8 +224,8 @@ struct Game
|
|||
SDL_SetMainReady();
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
||||
{
|
||||
print_sdl_error("SDL could not initalize!");
|
||||
return false;
|
||||
print_sdl_error("SDL could not initialize");
|
||||
done = true;
|
||||
}
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
|
@ -218,31 +234,52 @@ struct Game
|
|||
sw, sh, SDL_WINDOW_OPENGL);
|
||||
if (window == NULL)
|
||||
{
|
||||
print_sdl_error("Could not create window!");
|
||||
return false;
|
||||
print_sdl_error("Could not create window");
|
||||
done = true;
|
||||
}
|
||||
SDL_ShowCursor(0);
|
||||
renderer = SDL_CreateRenderer(window, -1, 0);
|
||||
if (renderer == NULL)
|
||||
{
|
||||
print_sdl_error("Could not create renderer!");
|
||||
return false;
|
||||
}
|
||||
SDL_Texture *texture = IMG_LoadTexture(renderer, "background.png");
|
||||
if (!texture)
|
||||
{
|
||||
print_sdl_error("Could not load image!");
|
||||
return false;
|
||||
}
|
||||
if (TTF_Init() < 0)
|
||||
{
|
||||
print_sdl_error("Could not initialize SDL ttf!");
|
||||
return false;
|
||||
print_sdl_error("Could not initialize SDL ttf");
|
||||
done = true;
|
||||
}
|
||||
load_gl_context();
|
||||
}
|
||||
|
||||
void print_sdl_error(std::string message)
|
||||
{
|
||||
std::cerr << message << " " << SDL_GetError() << std::endl;
|
||||
}
|
||||
|
||||
void load_sdl_context()
|
||||
{
|
||||
if (glcontext != NULL)
|
||||
{
|
||||
SDL_GL_DeleteContext(glcontext);
|
||||
}
|
||||
if ((renderer = SDL_CreateRenderer(window, -1, 0)) == NULL)
|
||||
{
|
||||
print_sdl_error("Could not create renderer");
|
||||
done = true;
|
||||
}
|
||||
if (!(grass_texture = IMG_LoadTexture(renderer, "resource/Field.png")))
|
||||
{
|
||||
print_sdl_error("Could not load image");
|
||||
done = true;
|
||||
}
|
||||
is_gl_context = false;
|
||||
}
|
||||
|
||||
void load_gl_context()
|
||||
{
|
||||
if (renderer != NULL)
|
||||
{
|
||||
SDL_DestroyRenderer(renderer);
|
||||
}
|
||||
if ((glcontext = SDL_GL_CreateContext(window)) == NULL)
|
||||
{
|
||||
print_sdl_error("Could not get GL context!");
|
||||
return false;
|
||||
print_sdl_error("Could not get GL context");
|
||||
done = true;
|
||||
}
|
||||
printf("OpenGL %s, renderer %s, shading language %s\n", glGetString(GL_VERSION),
|
||||
glGetString(GL_RENDERER), glGetString(GL_SHADING_LANGUAGE_VERSION));
|
||||
|
@ -272,7 +309,7 @@ struct Game
|
|||
{-1,-1,-1}, {-1,-1, 1}, {-1, 1, 1},
|
||||
{-1,-1,-1}, {1,-1,-1}, {1,-1, 1},
|
||||
{1,-1, 1}, {-1,-1, 1}, {-1,-1,-1},
|
||||
{1,-1,-1}, {-1,-1,-1}, {-1, 1,-1},
|
||||
{1,-1,-1}, {-1,-1,-1}, {-1, 1,-1},
|
||||
{-1, 1,-1}, {1, 1,-1}, {1,-1,-1}
|
||||
}};
|
||||
std::array<glm::vec3, 6> background_vertices = {
|
||||
|
@ -300,11 +337,10 @@ struct Game
|
|||
vertices.insert(vertices.end(), framerate_indicator_vertices.begin(),
|
||||
framerate_indicator_vertices.end());
|
||||
projection = glm::perspective(glm::radians(45.0f),
|
||||
(float) sw / (float) sh, 0.1f, 100.0f);
|
||||
(float) sw / (float) sh, 0.1f, 100.0f);
|
||||
view = glm::lookAt(
|
||||
glm::vec3(4, 3, 3), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
|
||||
model = glm::mat4(1.0f);
|
||||
SDL_Surface *surface = rotateSurface90Degrees(IMG_Load("tile.png"), 2);
|
||||
SDL_Surface *surface = rotateSurface90Degrees(IMG_Load("resource/tile.png"), 2);
|
||||
printf("tile.png bytes per pixel %i\n", surface->format->BytesPerPixel);
|
||||
space_texture_id = get_gl_texture_from_surface(surface, GL_LINEAR);
|
||||
std::array<glm::vec2, 6> framerate_indicator_uv = {
|
||||
|
@ -368,6 +404,7 @@ struct Game
|
|||
glBindAttribLocation(world_program, 0, "in_Position");
|
||||
glBindAttribLocation(world_program, 1, "vertexUV");
|
||||
glBindAttribLocation(world_program, 2, "in_Color");
|
||||
glBindAttribLocation(world_program, 3, "pos");
|
||||
link_shader(world_program);
|
||||
flat_program = glCreateProgram();
|
||||
glAttachShader(flat_program, flat_shader);
|
||||
|
@ -375,6 +412,7 @@ struct Game
|
|||
glBindAttribLocation(flat_program, 0, "in_Position");
|
||||
glBindAttribLocation(flat_program, 1, "vertexUV");
|
||||
glBindAttribLocation(flat_program, 2, "in_Color");
|
||||
glBindAttribLocation(flat_program, 3, "pos");
|
||||
link_shader(flat_program);
|
||||
mvp_id = glGetUniformLocation(world_program, "MVP");
|
||||
GLuint sampler_uniform_id = glGetUniformLocation(world_program, "myTextureSampler");
|
||||
|
@ -385,12 +423,7 @@ struct Game
|
|||
last_frame_timestamp = frame_count_timestamp = last_capture_timestamp = SDL_GetTicks();
|
||||
framerate_texture_id = get_gl_texture_from_surface(
|
||||
get_framerate_indicator_surface(frame_count), GL_LINEAR);
|
||||
return true;
|
||||
}
|
||||
|
||||
void print_sdl_error(std::string message)
|
||||
{
|
||||
std::cerr << message << SDL_GetError() << std::endl;
|
||||
is_gl_context = true;
|
||||
}
|
||||
|
||||
void run()
|
||||
|
@ -459,6 +492,17 @@ struct Game
|
|||
{
|
||||
show_framerate = !show_framerate;
|
||||
}
|
||||
else if (event.key.keysym.sym == SDLK_SPACE)
|
||||
{
|
||||
if (is_gl_context)
|
||||
{
|
||||
load_sdl_context();
|
||||
}
|
||||
else
|
||||
{
|
||||
load_gl_context();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (is_recording && ticks - last_capture_timestamp + capture_time_overflow >
|
||||
|
@ -475,42 +519,51 @@ struct Game
|
|||
fprintf(stderr, "lost %i frame(s) during capture\n", ii);
|
||||
}
|
||||
}
|
||||
// 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);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glUseProgram(flat_program);
|
||||
glBindTexture(GL_TEXTURE_2D, fake_texture_id);
|
||||
glDisableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
glDrawArrays(GL_TRIANGLES, 36, 6);
|
||||
if (show_framerate)
|
||||
if (is_gl_context)
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D, framerate_texture_id);
|
||||
glDisableVertexAttribArray(2);
|
||||
// glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
|
||||
// glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), diamond, GL_STATIC_DRAW);
|
||||
glClearColor(.7, .7, .5, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glUseProgram(flat_program);
|
||||
glUniform1f(glGetUniformLocation(flat_program, "r"), mvp[0][0]);
|
||||
glBindTexture(GL_TEXTURE_2D, fake_texture_id);
|
||||
glDisableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
glDrawArrays(GL_TRIANGLES, 36, 6);
|
||||
if (show_framerate)
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D, framerate_texture_id);
|
||||
glDisableVertexAttribArray(2);
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttrib3f(2, 1, 1, 1);
|
||||
glDrawArrays(GL_TRIANGLES, 42, 6);
|
||||
}
|
||||
// printf("%s\n", glm::to_string(model).c_str());
|
||||
model = glm::rotate(model, .0005f * frame_length, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
mvp = projection * view * model;
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glUseProgram(world_program);
|
||||
glUniformMatrix4fv(mvp_id, 1, GL_FALSE, &mvp[0][0]);
|
||||
glBindTexture(GL_TEXTURE_2D, space_texture_id);
|
||||
glEnableVertexAttribArray(1);
|
||||
glDisableVertexAttribArray(2);
|
||||
glVertexAttrib3f(2, 1, 1, 1);
|
||||
glDrawArrays(GL_TRIANGLES, 42, 6);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
//glFlush();
|
||||
SDL_GL_SwapWindow(window);
|
||||
}
|
||||
else
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer, 0x0f, 0x4f, 0x8f, 0xff);
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_Rect rect = {0, 0, 240, 160};
|
||||
SDL_RenderCopy(renderer, grass_texture, NULL, &rect);
|
||||
roundedBoxColor(renderer, 300, 200, 500, 300, 10, 0x8f8fdfff);
|
||||
aacircleColor(renderer, 300, 200, 30, 0xffef3fff);
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
// printf("%s\n", glm::to_string(model).c_str());
|
||||
model = glm::rotate(model, .0005f * frame_length, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
mvp = projection * view * model;
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glUseProgram(world_program);
|
||||
glUniformMatrix4fv(mvp_id, 1, GL_FALSE, &mvp[0][0]);
|
||||
glBindTexture(GL_TEXTURE_2D, space_texture_id);
|
||||
glEnableVertexAttribArray(1);
|
||||
glDisableVertexAttribArray(2);
|
||||
glVertexAttrib3f(2, 1, 1, 1);
|
||||
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);
|
||||
}
|
||||
|
||||
void quit()
|
||||
|
@ -539,10 +592,7 @@ struct Game
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
Game g = Game();
|
||||
if (g.initialize())
|
||||
{
|
||||
g.run();
|
||||
}
|
||||
g.run();
|
||||
g.quit();
|
||||
return 0;
|
||||
}
|
||||
|
|
Before Width: | Height: | Size: 5.5 KiB After Width: | Height: | Size: 5.5 KiB |
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 3.8 KiB |
Before Width: | Height: | Size: 703 B After Width: | Height: | Size: 703 B |
|
@ -2,9 +2,10 @@
|
|||
|
||||
in vec4 ex_Color;
|
||||
in vec2 UV;
|
||||
in vec4 pos;
|
||||
uniform sampler2D myTextureSampler;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
gl_FragColor = texture(myTextureSampler, UV) * ex_Color;
|
||||
gl_FragColor = texture(myTextureSampler, UV) * ex_Color + pos;
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ in vec2 vertexUV;
|
|||
|
||||
out vec4 ex_Color;
|
||||
out vec2 UV;
|
||||
out vec4 pos;
|
||||
|
||||
uniform mat4 MVP;
|
||||
|
||||
|
@ -14,4 +15,5 @@ void main(void)
|
|||
gl_Position = MVP * vec4(in_Position, 1);
|
||||
ex_Color = vec4(in_Color, 1);
|
||||
UV = vertexUV;
|
||||
pos = vec4(vec3(MVP[0][0], MVP[0][1], MVP[0][2]) * vec3(UV, UV[0]), 0);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue