Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
frankkn committed Dec 22, 2023
1 parent cf6eadb commit 0b2d088
Show file tree
Hide file tree
Showing 199 changed files with 5,527 additions and 0 deletions.
77 changes: 77 additions & 0 deletions Final project/Demo/about.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "about.h"

ALLEGRO_FONT *about_font = NULL;
ALLEGRO_BITMAP *about1, *about2, *about3, *about4, *aboutImage;
int aboutStatus;

void about_init()
{
aboutStatus = 0;
about_font = al_load_ttf_font("./font/pirulen.ttf", 40,0);
about1 = al_load_bitmap("./image/about1.jpg");
about2 = al_load_bitmap("./image/about2.jpg");
about3 = al_load_bitmap("./image/about3.jpg");
about4 = al_load_bitmap("./image/about4.jpg");
}

void about_draw()
{
al_draw_scaled_bitmap(aboutImage, 0, 0,
al_get_bitmap_width(aboutImage),
al_get_bitmap_height(aboutImage),
0, 0, GAME_WIDTH, GAME_HEIGHT, 0);
al_draw_text(about_font, al_map_rgb(255,255,255), GAME_WIDTH/2, GAME_HEIGHT - 100, ALLEGRO_ALIGN_CENTRE,
"Press backspace to return to the menu.");
}

void about_update()
{
if( key_state[ALLEGRO_KEY_UP] ){
aboutStatus = (aboutStatus-1+4) % 4;
}else if( key_state[ALLEGRO_KEY_DOWN] ){
aboutStatus = (aboutStatus+1) % 4;
}
if( key_state[ALLEGRO_KEY_BACKSPACE] ){
judge_next_window = true;
status = 1;
}
switch(aboutStatus)
{
case 0:
aboutImage = about1;
break;
case 1:
aboutImage = about2;
break;
case 2:
aboutImage = about3;
break;
case 3:
aboutImage = about4;
break;
}
}

void about_process(ALLEGRO_EVENT event)
{
if( event.type == ALLEGRO_EVENT_KEY_DOWN ){
key_state[event.keyboard.keycode] = true;
}else if( event.type == ALLEGRO_EVENT_KEY_UP ){
key_state[event.keyboard.keycode] = false;
}
/*if( event.type == ALLEGRO_EVENT_KEY_UP )
if( event.keyboard.keycode == ALLEGRO_KEY_BACKSPACE ){
judge_next_window = true;
status = 1;
}*/
}

void about_destroy()
{
al_destroy_font(about_font);
al_destroy_bitmap(about1);
al_destroy_bitmap(about2);
al_destroy_bitmap(about3);
al_destroy_bitmap(about4);
//al_destroy_bitmap(aboutImage);
}
7 changes: 7 additions & 0 deletions Final project/Demo/about.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "constants.h"

void about_init();
void about_draw();
void about_process(ALLEGRO_EVENT event);
void about_update();
void about_destroy();
26 changes: 26 additions & 0 deletions Final project/Demo/algif5/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
CFLAGS = -Isrc -Wall -Wextra
LDLIBS = `pkg-config allegro_monolith-debug-static-5 --libs --static`
.PHONY: all
all: examples/example
examples/example: examples/example.o src/gif.o src/lzw.o src/bitmap.o src/algif.o
index.html: readme.md
cp readme.md index.html
algif5.zip: index.html
mkdir algif5
cp Makefile index.html algif5/
mkdir algif5/examples
cp examples/*.c algif5/examples
cp examples/*.gif algif5/examples
mkdir algif5/src
cp src/*.c algif5/src
cp src/*.h algif5/src
zip -r algif5.zip algif5
rm -r algif5

clean:
${RM} src/*.o
${RM} examples/*.o
${RM} examples/example
${RM} allegro.log
${RM} index.html
${RM} algif5.zip
109 changes: 109 additions & 0 deletions Final project/Demo/algif5/algif.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "algif.h"
#include <allegro5/allegro_primitives.h>
#include <math.h>

/* Renders the next frame in a GIF animation to the given position.
* You need to call this in order on the same destination for frames
* [0..gif->frames_count - 1] to properly render all the frames in the GIF.
* The current target bitmap should have the same height as the animation,
* and blending should be set to fully copy RGBA.
*/
void algif_render_frame(ALGIF_ANIMATION *gif, int frame, int xpos, int ypos) {
int x, y, w, h;
ALGIF_FRAME *f = &gif->frames[frame];
ALGIF_PALETTE *pal;
if (frame == 0) {
al_draw_filled_rectangle(xpos, ypos, xpos + gif->width,
ypos + gif->height, al_map_rgba_f(0, 0, 0, 0));
}
else {
ALGIF_FRAME *p = &gif->frames[frame - 1];
if (p->disposal_method == 2) {
al_draw_filled_rectangle(xpos + p->xoff, ypos + p->yoff,
xpos + p->xoff + p->bitmap_8_bit->w,
ypos + p->yoff + p->bitmap_8_bit->h,
al_map_rgba_f(0, 0, 0, 0));
}
else if (p->disposal_method == 3 && gif->store) {
al_draw_bitmap_region(gif->store, xpos + p->xoff, ypos + p->yoff,
p->bitmap_8_bit->w,
p->bitmap_8_bit->h,
xpos + p->xoff, ypos + p->yoff, 0);
al_destroy_bitmap(gif->store);
gif->store = NULL;
}
}
w = f->bitmap_8_bit->w;
h = f->bitmap_8_bit->h;
if (f->disposal_method == 3) {
if (gif->store)
al_destroy_bitmap(gif->store);
gif->store = al_clone_bitmap(al_get_target_bitmap());
}
pal = &gif->frames[frame].palette;
if (pal->colors_count == 0)
pal = &gif->palette;

for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
int c = f->bitmap_8_bit->data[x + y * f->bitmap_8_bit->w];
if (c != f->transparent_index) {
al_draw_pixel(xpos + f->xoff + x, ypos + f->yoff + y,
al_map_rgb(pal->colors[c].r, pal->colors[c].g,
pal->colors[c].b));
}
}
}
}

ALGIF_ANIMATION *algif_load_animation_f(ALLEGRO_FILE *file) {
ALGIF_ANIMATION *gif = algif_load_raw(file);

if (!gif)
return gif;

al_init_primitives_addon();

gif->duration = 0;
ALLEGRO_STATE s;
al_store_state(&s, ALLEGRO_STATE_TARGET_BITMAP | ALLEGRO_STATE_BLENDER);
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO);
int n = gif->frames_count;
int i;
for (i = 0; i < n; i++) {
ALGIF_FRAME *f = &gif->frames[i];
f->rendered = al_create_bitmap(gif->width, gif->height);
al_set_target_bitmap(f->rendered);
algif_render_frame(gif, i, 0, 0);
gif->duration += f->duration;
}

al_restore_state(&s);
return gif;
}

ALGIF_ANIMATION *algif_load_animation(char const *filename) {
ALLEGRO_FILE *file = al_fopen(filename, "rb");
return algif_load_animation_f(file);
}

ALLEGRO_BITMAP *algif_get_bitmap(ALGIF_ANIMATION *gif, double seconds) {
int n = gif->frames_count;
seconds = fmod(seconds, gif->duration / 100.0);
double d = 0;
int i;
for (i = 0; i < n; i++) {
d += gif->frames[i].duration / 100.0;
if (seconds < d)
return gif->frames[i].rendered;
}
return gif->frames[0].rendered;
}

ALLEGRO_BITMAP *algif_get_frame_bitmap(ALGIF_ANIMATION *gif, int i) {
return gif->frames[i].rendered;
}

double algif_get_frame_duration(ALGIF_ANIMATION *gif, int i) {
return gif->frames[i].duration / 100.0;
}
71 changes: 71 additions & 0 deletions Final project/Demo/algif5/algif.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#ifndef _GIF_H_
#define _GIF_H_

#include <allegro5/allegro5.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct ALGIF_ANIMATION ALGIF_ANIMATION;
typedef struct ALGIF_FRAME ALGIF_FRAME;
typedef struct ALGIF_PALETTE ALGIF_PALETTE;
typedef struct ALGIF_BITMAP ALGIF_BITMAP;
typedef struct ALGIF_RGB ALGIF_RGB;

struct ALGIF_RGB {
uint8_t r, g, b;
};

struct ALGIF_PALETTE {
int colors_count;
ALGIF_RGB colors[256];
};

struct ALGIF_BITMAP {
int w, h;
uint8_t *data;
};

struct ALGIF_ANIMATION {
int width, height;
int frames_count;
int background_index;
int loop; /* -1 = no, 0 = forever, 1..65535 = that many times */
ALGIF_PALETTE palette;
ALGIF_FRAME *frames;

int duration;
ALLEGRO_BITMAP *store;
};

struct ALGIF_FRAME {
ALGIF_BITMAP *bitmap_8_bit;
ALGIF_PALETTE palette;
int xoff, yoff;
int duration; /* in 1/100th seconds */
int disposal_method; /* 0 = don't care, 1 = keep, 2 = background, 3 = previous */
int transparent_index;

ALLEGRO_BITMAP *rendered;
};

ALGIF_ANIMATION *algif_load_raw(ALLEGRO_FILE *file);
ALGIF_ANIMATION *algif_load_animation_f(ALLEGRO_FILE *file);
ALGIF_ANIMATION *algif_load_animation(char const *filename);
void algif_render_frame(ALGIF_ANIMATION *gif, int frame, int xpos, int ypos);
void algif_destroy_animation (ALGIF_ANIMATION *gif);

ALGIF_BITMAP *algif_create_bitmap(int w, int h);
void algif_destroy_bitmap(ALGIF_BITMAP *bitmap);
void algif_blit(ALGIF_BITMAP *from, ALGIF_BITMAP *to, int xf, int yf, int xt, int yt,
int w, int h);
ALLEGRO_BITMAP *algif_get_bitmap(ALGIF_ANIMATION *gif, double seconds);
ALLEGRO_BITMAP *algif_get_frame_bitmap(ALGIF_ANIMATION *gif, int i);
double algif_get_frame_duration(ALGIF_ANIMATION *gif, int i);

#ifdef __cplusplus
}
#endif

#endif
74 changes: 74 additions & 0 deletions Final project/Demo/algif5/bitmap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "algif.h"

ALGIF_BITMAP *algif_create_bitmap(int w, int h) {
ALGIF_BITMAP *bitmap = calloc(1, sizeof *bitmap);
bitmap->w = w;
bitmap->h = h;
bitmap->data = calloc(1, w * h);
return bitmap;
}

void algif_destroy_bitmap(ALGIF_BITMAP *bitmap) {
free(bitmap->data);
free(bitmap);
}

void algif_blit(ALGIF_BITMAP *from, ALGIF_BITMAP *to, int xf, int yf, int xt, int yt,
int w, int h) {

if (w <= 0 || h <= 0)
return;

/* source clipping */
if (xf < 0) {
w += xf;
xt -= xf;
xf = 0;
}
if (yf < 0) {
h += yf;
yt -= yf;
yf = 0;
}
int wf = from->w;
int hf = from->h;
if (xf + w > wf) {
w = wf - xf;
}
if (yf + h > hf) {
h = hf - yf;
}

/* destination clipping */
if (xt < 0) {
w += xt;
xf -= xt;
xt = 0;
}
if (yt < 0) {
h += yt;
yf -= yt;
yt = 0;
}
int wt = to->w;
int ht = to->h;
if (xt + w > wt) {
w = wt - xt;
}
if (yt + h > ht) {
h = ht - yt;
}

if (w <= 0 || h <= 0)
return;

/* copy row by row */
uint8_t *pf = from->data + yf * from->w;
uint8_t *pt = to->data + yt * to->w;
int i;
for (i = 0; i < h; i++) {
memmove(pt + xt, pf + xf, w);
pf += from->w;
pt += to->w;
}
}
Loading

0 comments on commit 0b2d088

Please sign in to comment.