1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
#include <cstdio>
#include "allegro5/allegro5.h"
#include "allegro5/allegro_image.h"
#include "allegro5/allegro_primitives.h"
/* The ALLEGRO_CFG_* defines are actually internal to Allegro so don't use them
* in your own programs.
*/
#ifdef ALLEGRO_CFG_D3D
#include "allegro5/allegro_direct3d.h"
#endif
#ifdef ALLEGRO_CFG_OPENGL
#include "allegro5/allegro_opengl.h"
#endif
#include "common.c"
static int display_flags = 0;
static void parse_args(int argc, char **argv)
{
int i;
for (i = 1; i < argc; i++) {
if (0 == strcmp(argv[i], "--opengl")) {
display_flags = ALLEGRO_OPENGL;
continue;
}
#ifdef ALLEGRO_CFG_D3D
if (0 == strcmp(argv[i], "--d3d")) {
display_flags = ALLEGRO_DIRECT3D;
continue;
}
#endif
abort_example("Unrecognised argument: %s\n", argv[i]);
}
}
static void choose_shader_source(ALLEGRO_SHADER *shader,
char const **vsource, char const **psource)
{
ALLEGRO_SHADER_PLATFORM platform = al_get_shader_platform(shader);
if (platform == ALLEGRO_SHADER_HLSL) {
*vsource = "data/ex_shader_vertex.hlsl";
*psource = "data/ex_shader_pixel.hlsl";
}
else if (platform == ALLEGRO_SHADER_GLSL) {
*vsource = "data/ex_shader_vertex.glsl";
*psource = "data/ex_shader_pixel.glsl";
}
else {
/* Shouldn't happen. */
*vsource = NULL;
*psource = NULL;
}
}
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display;
ALLEGRO_BITMAP *bmp;
ALLEGRO_SHADER *shader;
const char *vsource;
const char *psource;
parse_args(argc, argv);
if (!al_init()) {
abort_example("Could not init Allegro.\n");
}
al_install_keyboard();
al_init_image_addon();
init_platform_specific();
al_set_new_display_flags(ALLEGRO_PROGRAMMABLE_PIPELINE | display_flags);
display = al_create_display(640, 480);
if (!display) {
abort_example("Could not create display.\n");
}
bmp = al_load_bitmap("data/mysha.pcx");
if (!bmp) {
abort_example("Could not load bitmap.\n");
}
shader = al_create_shader(ALLEGRO_SHADER_AUTO);
if (!shader) {
abort_example("Could not create shader.\n");
}
choose_shader_source(shader, &vsource, &psource);
if (!vsource|| !psource) {
abort_example("Could not load source files.\n");
}
if (!al_attach_shader_source_file(shader, ALLEGRO_VERTEX_SHADER, vsource)) {
abort_example("al_attach_shader_source_file failed: %s\n",
al_get_shader_log(shader));
}
if (!al_attach_shader_source_file(shader, ALLEGRO_PIXEL_SHADER, psource)) {
abort_example("al_attach_shader_source_file failed: %s\n",
al_get_shader_log(shader));
}
if (!al_build_shader(shader)) {
abort_example("al_build_shader failed: %s\n", al_get_shader_log(shader));
}
al_use_shader(shader);
float tints[12] = {
4.0, 0.0, 1.0,
0.0, 4.0, 1.0,
1.0, 0.0, 4.0,
4.0, 4.0, 1.0
};
while (1) {
ALLEGRO_KEYBOARD_STATE s;
al_get_keyboard_state(&s);
if (al_key_down(&s, ALLEGRO_KEY_ESCAPE))
break;
al_clear_to_color(al_map_rgb(140, 40, 40));
al_set_shader_float_vector("tint", 3, &tints[0], 1);
al_draw_bitmap(bmp, 0, 0, 0);
al_set_shader_float_vector("tint", 3, &tints[3], 1);
al_draw_bitmap(bmp, 320, 0, 0);
al_set_shader_float_vector("tint", 3, &tints[6], 1);
al_draw_bitmap(bmp, 0, 240, 0);
/* Draw the last one transformed */
ALLEGRO_TRANSFORM trans, backup;
al_copy_transform(&backup, al_get_current_transform());
al_identity_transform(&trans);
al_translate_transform(&trans, 320, 240);
al_set_shader_float_vector("tint", 3, &tints[9], 1);
al_use_transform(&trans);
al_draw_bitmap(bmp, 0, 0, 0);
al_use_transform(&backup);
al_flip_display();
al_rest(0.01);
}
al_use_shader(NULL);
al_destroy_shader(shader);
return 0;
}
/* vim: set sts=3 sw=3 et: */
|