Skip to content

Instantly share code, notes, and snippets.

@lukas-h
Created May 1, 2019 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukas-h/289324f70752249b6bfe4060ff661aa0 to your computer and use it in GitHub Desktop.
Save lukas-h/289324f70752249b6bfe4060ff661aa0 to your computer and use it in GitHub Desktop.
Snippet for allegro 4 game engine to create a basic 2d game
/*
* snippet.c
*
* Copyright (C) 2015, 2016 Lukas Himsel <lukas.himsel@web.de>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdio.h>
#include <allegro.h>
#include <pthread.h>
#define NUM_THREADS 2
#define WIDTH 1920U
#define HEIGHT 1080U
#define WINDOWED 1
/* Activate WINDOWED, if you want a window, not fullscreen! */
BITMAP *buffer;
BITMAP *cursor; //mouse cursor
int content_loaded=0;
static int rungame(void);
static void* loading_screen(void *arg); //called as thread
static void* load_contents(void *arg); //called as thread
static void update(void);
static int init(void);
static int double_buffering_init(void); //used by init()
static void exit_error(void);
int main(int argc, char *argv[]){
int rc;
void *status[NUM_THREADS];
pthread_t threads[NUM_THREADS];
/* init */
if(init()){
return 1;
}
/* content thread */
rc=pthread_create(&threads[0], NULL, loading_screen, (void *)0);
rc=pthread_create(&threads[1], NULL, load_contents, (void *)0);
if(rc){
exit_error();
}
rc=pthread_join(theads[0], &status[0]);
rc=pthread_join(threads[1], &status[1]);
/* game action */
if(rungame()){
return 1;
}
/* exit */
destroy_bitmap(cursor);
destroy_bitmap(buffer);
allegro_exit();
pthread_exit(NULL);
}
END_OF_MAIN();
/* Game action-part */
static int rungame(){
int k;
while(!key[KEY_ESC]){
clear(buffer);// clear complete buffer
/* print */
textprintf_ex(buffer,font,(SCREEN_W / 2 -30),(SCREEN_H / 2 -30),makecol(255,0,0),makecol(0,0,0),"HELLO WORLD!");
/* draw mouse cursor */
draw_sprite(buffer,cursor,mouse_x,mouse_y);
/* events */
// mouse
if((mouse_b&1)==1){ // left mouse button
}
if((mouse_b&2)==2){ // right mouse button
}
//key
if(k=readkey()){
switch(k){
//case 'W':
case KEY_UP:
break;
//case 'A':
case KEY_LEFT:
break;
//case 'S':
case KEY_DOWN:
break;
//case 'D':
case KEY_RIGHT:
break;
case KEY_ENTER:
break;
}
}
update();// update screen
}
return 0;
}
static void* loading_screen(void *arg){
BITMAP *backgd;
backgd = load_bitmap("<loading screen.bmp>", NULL);
while(content_loaded==0){
clear(buffer);
draw_sprite(buffer,backgd,0,0);
update();
}
destroy_bitmap(backgd);
pthread_exit(NULL);
}
static void* load_contents(void *arg){
/*
* loading contents like bitmaps, samples, etc.
* The content has to be declared globally.
*/
cursor = load_bitmap("<cursor.bmp>", NULL);
content_loaded=1;
pthread_exit(NULL);
}
static void update(){
blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}
static int init(){
allegro_init();
install_keyboard();
install_mouse();
install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL);
set_color_depth(32);
#ifndef WINDOWED
set_gfx_mode(GFX_AUTODETECT, WIDTH, HEIGHT, 0, 0);
#else
set_gfx_mode(GFX_AUTODETECT_WINDOWED, WIDTH, HEIGHT, 0, 0);
#endif
set_window_title("<title>");
if (double_buffering_init()){
exit_error();
}
return 0;
}
static int double_buffering_init(){
buffer = create_bitmap(SCREEN_W, SCREEN_H);
if(!buffer)
return 1;
return 0;
}
static void exit_error(){
set_gfx_mode(GFX_TEXT,0,0,0,0);
destroy_bitmap(buffer);
destroy_bitmap(cursor);
printf("Program crashed");
exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment