Created
December 21, 2024 14:41
-
-
Save ryonagana/edc3fd69ccd72b487ca93927449a2833 to your computer and use it in GitHub Desktop.
arvore de natal teste
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <allegro5/allegro.h> | |
#include <allegro5/allegro_image.h> | |
#include <allegro5/allegro_primitives.h> | |
#include <allegro5/allegro_opengl.h> | |
#include <allegro5/allegro_audio.h> | |
#include <allegro5/allegro_acodec.h> | |
#include <allegro5/allegro_ttf.h> | |
#include <allegro5/allegro_native_dialog.h> | |
#include <cstdio> | |
#include <cstdlib> | |
#define UNUSED(x) (void(x)) | |
ALLEGRO_DISPLAY *display = nullptr; | |
ALLEGRO_EVENT_QUEUE *queue = nullptr; | |
ALLEGRO_TIMER *timer = nullptr; | |
ALLEGRO_BITMAP *screen = nullptr; | |
typedef struct vertex_data { | |
float x; | |
float y; | |
int id; | |
}vertex_data; | |
ALLEGRO_BITMAP *star_spr = nullptr; | |
ALLEGRO_SHADER *snow_effect = nullptr; | |
int sw = 0; | |
int sh = 0; | |
void draw_vtx(vertex_data v[], int count, ALLEGRO_COLOR c){ | |
al_draw_polyline( reinterpret_cast<float*>(v), sizeof(vertex_data), count, ALLEGRO_LINE_JOIN_NONE, ALLEGRO_LINE_CAP_NONE, c, 1.0, 1.0); | |
} | |
void star(float x, float y){ | |
al_draw_scaled_bitmap(star_spr, 0,0, al_get_bitmap_width(star_spr), al_get_bitmap_height(star_spr), | |
x,y, 64,64,0); | |
} | |
void trunk(float x, float y){ | |
//int SW = al_get_display_width(display); | |
//nt SH = al_get_display_height(display); | |
al_draw_filled_rectangle(x, y, x + 70, y + 400, al_map_rgb(150,75,0)); | |
} | |
void foliage(float x, float y){ | |
ALLEGRO_VERTEX v[3]; | |
v[0].x = (al_get_display_width(display) / 2) + x; | |
v[0].y = y; | |
v[0].z = 0.0; | |
v[0].color = al_map_rgb(0,255,0); | |
v[0].u = 128; | |
v[0].v = 0; | |
v[1].x = (al_get_display_width(display) / 2)-100 + x; | |
v[1].y = al_get_display_height(display) / 2 + y; | |
v[1].z = 0.0; | |
v[1].color = al_map_rgb(0,255,0); | |
v[2].x = al_get_display_width(display)-100 + x; | |
v[2].y = al_get_display_height(display) / 2 + y; | |
v[2].z = 0.0; | |
v[2].color = al_map_rgb(0,255,0); | |
al_draw_prim(v, nullptr, nullptr, 0,3, ALLEGRO_PRIM_TRIANGLE_LIST); | |
} | |
int main(int argc, char **argv){ | |
UNUSED(argc); | |
UNUSED(argv); | |
if(!al_init()){ | |
fprintf(stderr, "%s - %d- Allegro Failed to Init!", __FILE__, __LINE__); | |
al_uninstall_system(); | |
exit(1); | |
} | |
if(!al_install_keyboard()){ | |
fprintf(stderr, "%s - %d- Keyboard Failed!", __FILE__, __LINE__); | |
al_uninstall_system(); | |
exit(1); | |
} | |
if(!al_install_mouse()){ | |
fprintf(stderr, "%s - %d- Mouse Failed!", __FILE__, __LINE__); | |
al_uninstall_system(); | |
exit(1); | |
} | |
if(!al_install_audio()){ | |
fprintf(stderr, "%s - %d- Audio Failed!", __FILE__, __LINE__); | |
al_uninstall_system(); | |
exit(1); | |
} | |
al_init_image_addon(); | |
al_init_primitives_addon(); | |
al_init_acodec_addon(); | |
al_init_ttf_addon(); | |
al_set_new_display_option(ALLEGRO_WINDOWED, 1, ALLEGRO_SUGGEST); | |
al_set_new_display_flags(ALLEGRO_OPENGL_FORWARD_COMPATIBLE|ALLEGRO_PROGRAMMABLE_PIPELINE|ALLEGRO_GENERATE_EXPOSE_EVENTS); | |
display = al_create_display(800,600); | |
al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); | |
al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST); | |
if(display == nullptr) { | |
al_show_native_message_box(display, "Error!", "Display Failed!", "Display is NULL", nullptr, 0); | |
al_uninstall_system(); | |
exit(1); | |
} | |
sw = al_get_display_width(display); | |
sh = al_get_display_height(display); | |
queue = al_create_event_queue(); | |
if(queue == nullptr){ | |
al_show_native_message_box(display, "Error!", "Event Queue Failed!", "Display is NULL", nullptr, 0); | |
al_uninstall_system(); | |
exit(1); | |
} | |
timer = al_create_timer(1.0/60.0); | |
if(timer == nullptr){ | |
al_show_native_message_box(display, "Error!", "Timer Failed!", "Display is NULL", nullptr, 0); | |
al_uninstall_system(); | |
exit(1); | |
} | |
int old_flags = al_get_display_flags(display); | |
al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP | ALLEGRO_NO_PRESERVE_TEXTURE); | |
screen = al_create_bitmap(al_get_display_width(display), al_get_display_height(display)); | |
al_set_new_bitmap_flags(old_flags); | |
al_register_event_source(queue, al_get_display_event_source(display)); | |
al_register_event_source(queue, al_get_keyboard_event_source()); | |
al_register_event_source(queue, al_get_mouse_event_source()); | |
al_register_event_source(queue, al_get_timer_event_source(timer)); | |
al_start_timer(timer); | |
bool started = true; | |
bool redraw = false; | |
star_spr = al_load_bitmap("star.png"); | |
if(star_spr == nullptr){ | |
fprintf(stderr, "star.png not loaded!\n\n"); | |
} | |
snow_effect = al_create_shader(ALLEGRO_SHADER_GLSL); | |
//const char *default_vert_shader = al_get_default_shader_source(ALLEGRO_SHADER_GLSL, ALLEGRO_VERTEX_SHADER); | |
if(!al_attach_shader_source_file(snow_effect, ALLEGRO_VERTEX_SHADER, "snow.vert" )){ | |
char log[2048]; | |
snprintf(log,2048,"ERROR: %s", al_get_shader_log(snow_effect)); | |
al_show_native_message_box(display, "Vert Error!", "Error!", log, nullptr, 0); | |
} | |
if(!al_attach_shader_source_file(snow_effect, ALLEGRO_PIXEL_SHADER, "snow.frag")){ | |
char log[2048]; | |
snprintf(log,2048,"ERROR: %s", al_get_shader_log(snow_effect)); | |
al_show_native_message_box(display, "Frag Error", "Error!", log, nullptr, 0); | |
} | |
if(!al_build_shader(snow_effect)){ | |
al_show_native_message_box(display, "Error!", "Error!", "Shader Build Error", nullptr, 0); | |
} | |
float res[2]; | |
res[0] = static_cast<float>(sw); | |
res[1] = static_cast<float>(sh); | |
while(started){ | |
ALLEGRO_EVENT e; | |
if(al_event_queue_is_empty(queue) && redraw){ | |
redraw = false; | |
al_clear_to_color(al_map_rgb(255,0,0)); | |
al_set_target_bitmap(screen); | |
al_use_shader(snow_effect); | |
al_set_shader_sampler("tex", screen,0); | |
al_set_shader_float_vector("resolution", 2, res,1); | |
al_set_shader_float("time", static_cast<float>(al_get_time())); | |
al_set_target_backbuffer(display); | |
trunk(sw/2 + 40,sh/2); | |
foliage(0,50); | |
foliage(0, 100); | |
foliage(0, 150); | |
foliage(0, 100); | |
foliage(0, 150); | |
foliage(0, 100); | |
star(sw/2 -25,0); | |
al_draw_bitmap(screen,0,0,0); | |
al_flip_display(); | |
} | |
do { | |
al_wait_for_event(queue, &e); | |
if(e.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ | |
started = false; | |
break; | |
} | |
if(e.type == ALLEGRO_EVENT_TIMER){ | |
redraw = true; | |
} | |
}while(!al_is_event_queue_empty(queue)); | |
} | |
al_use_shader(NULL); | |
al_destroy_display(display); | |
al_destroy_event_queue(queue); | |
al_destroy_timer(timer); | |
al_destroy_bitmap(screen); | |
al_uninstall_system(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uniform sampler2D tex; | |
uniform vec2 resolution; | |
uniform float time; | |
float snow(vec2 uv, float scale) { | |
float w = smoothstep(1.0, 0.0, -uv.y * (scale / 10.0)); | |
if (w < 0.1) { | |
return 0.0; | |
} | |
float c = time / scale; | |
// Fall to left: | |
// uv += clock; | |
uv.y += c; | |
uv.x -= c; | |
uv.y += c * 2.0; | |
uv.x += cos(uv.y + time * 0.5) / scale; | |
uv *= scale; | |
vec2 s = floor(uv); | |
vec2 f = fract(uv); | |
vec2 p = vec2(0.0); | |
float k = 3.0; | |
float d = 0.0; | |
p = 0.5 + 0.35 * sin(11.0 * fract(sin((s + p + scale) * mat2(7, 3, 6, 5)) * 5.0)) - f; | |
d = length(p); | |
k = min(d, k); | |
k = smoothstep(0.0, k, sin(f.x + f.y) * 0.01); | |
return k * w; | |
} | |
void main(void){ | |
float size = mix(min(resolution.x, resolution.y), max(resolution.x, resolution.y), 0.5); | |
vec2 uv = (gl_FragCoord.xy * 2.0 - resolution.xy) / size; | |
float c = smoothstep(1.0, 0.0, clamp(uv.y * 0.1 + 0.75, 0.0, 0.75)); | |
c += snow(uv, 30.0) * 0.3; | |
c += snow(uv, 20.0) * 0.5; | |
c += snow(uv, 15.0) * 0.8; | |
c += snow(uv, 10.0); | |
c += snow(uv, 8.0); | |
c += snow(uv, 6.0); | |
c += snow(uv, 5.0); | |
gl_FragColor = texture(tex,uv).rgba * vec4(vec3(c),1.0); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 330 | |
in vec2 position; | |
void main(){ | |
gl_Position = vec4(position,1.0,1.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment