Permalink
Browse files

init

  • Loading branch information...
0 parents commit 12bbb599cd882d62d241940bf8342dd75f76f4f1 @dorkster committed Nov 26, 2012
Showing with 1,443 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +674 −0 COPYING
  3. +34 −0 Makefile
  4. +28 −0 README
  5. BIN res/LCD_Solid.ttf
  6. BIN res/blocks.png
  7. BIN res/cursor.png
  8. +194 −0 src/block.c
  9. +54 −0 src/block.h
  10. +79 −0 src/draw.c
  11. +27 −0 src/draw.h
  12. +83 −0 src/game.c
  13. +29 −0 src/game.h
  14. +52 −0 src/main.c
  15. +119 −0 src/sys.c
  16. +67 −0 src/sys.h
@@ -0,0 +1,3 @@
+freeblocks
+.depend
+build/
674 COPYING

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,34 @@
+PROJNAME=freeblocks
+SRCDIR=src
+BUILDDIR=build
+SOURCES=$(shell ls $(SRCDIR)/*.c)
+OBJECTS=$(patsubst $(SRCDIR)%.c,$(BUILDDIR)%.o, $(SOURCES))
+
+CC=gcc
+CFLAGS+=-Wall -O2 -mms-bitfields -std=c99
+LDFLAGS+=-lSDL -lSDL_ttf -lSDL_image
+
+all: $(BUILDDIR) $(SOURCES) $(PROJNAME)
+
+$(BUILDDIR):
+ mkdir -p $(BUILDDIR)/
+
+depend: .depend
+
+.depend: $(SOURCES)
+ @rm -f ./.depend
+ @$(CC) $(CFLAGS) -MM $^ > ./.depend
+ @sed -i 's/^/$(BUILDDIR)\//' ./.depend
+
+-include .depend
+
+$(PROJNAME): $(OBJECTS)
+ $(CC) $(OBJECTS) $(LDFLAGS) $(CFLAGS) -o $@
+
+$(BUILDDIR)/%.o:
+ $(CC) $< -c $(CFLAGS) -o $@
+
+clean:
+ rm -rf $(BUILDDIR)/ $(PROJNAME)
+ @rm ./.depend
+
28 README
@@ -0,0 +1,28 @@
+# FreeBlocks
+-----------------------------------------------------------------------
+
+FreeBlocks is a puzzle game with similar gameplay to Tetris Attack.
+
+## Copyright & License
+
+FreeBlocks is Copyright (c) 2012 Justin Jacobs
+
+FreeBlocks' source code is released under the GNU GPL v3. Later versions are permitted.
+
+The graphics are by Kenney.nl, licensed under CC-BY 3.0.
+The included LCD Solid font is public domain.
+
+## Dependencies & Building
+
+* SDL
+* SDL_ttf
+* SDL_image
+
+To compile FreeBlocks, simply run `make` in the base directory.
+
+## Controls
+
+* Arrow keys = Move cursor
+* Z = Switch blocks
+* X = Manually bump up the stack
+* ESC = Quit
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,194 @@
+/*
+ FreeBlocks - A simple puzzle game, similar to Tetris Attack
+ Copyright (C) 2012 Justin Jacobs
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "block.h"
+#include "sys.h"
+
+Block blocks[ROWS][COLS];
+int clear_delay = 0;
+int bump_timer = 0;
+const int POINTS_PER_BLOCK = 20;
+
+void blockSet(int i, int j, bool alive, int color) {
+ blocks[i][j].x = j*BLOCK_SIZE;
+ blocks[i][j].y = i*BLOCK_SIZE;
+ blocks[i][j].alive = alive;
+ blocks[i][j].color = color;
+ blocks[i][j].matched = false;
+}
+
+void blockClear(int i, int j) {
+ blocks[i][j].alive = false;
+ blocks[i][j].color = 1;
+ blocks[i][j].matched = false;
+}
+
+void blockSwitch(int i, int j, int k, int l) {
+ if (blocks[i][j].matched || blocks[k][l].matched) return;
+
+ int b1_color = blocks[i][j].color;
+ int b1_alive = blocks[i][j].alive;
+ int b2_color = blocks[k][l].color;
+ int b2_alive = blocks[k][l].alive;
+
+ blocks[i][j].color = b2_color;
+ blocks[i][j].alive = b2_alive;
+ blocks[k][l].color = b1_color;
+ blocks[k][l].alive = b1_alive;
+}
+
+bool blockCompare(int i, int j, int k, int l) {
+ if (blocks[i][j].color != blocks[k][l].color) return false;
+ if (blocks[i][j].alive != blocks[k][l].alive) return false;
+ return true;
+}
+
+void blockInitAll() {
+ int i,j;
+ int new_color = -1;
+ int last_color = -1;
+
+ for(i=0;i<ROWS;i++) {
+ for(j=0;j<COLS;j++) {
+ blockSet(i,j,false,-1);
+ }
+ }
+
+ for(i=8;i<ROWS;i++) {
+ for(j=0;j<COLS;j++) {
+ new_color = rand() % 6;
+ while (new_color == last_color || new_color == blocks[i-1][j].color) {
+ new_color = rand() % 6;
+ }
+ last_color = new_color;
+ blockSet(i,j,true,new_color);
+ }
+ }
+}
+
+void blockLogic() {
+ blockGravity();
+ blockMatch();
+
+ if (bump_timer > 0) bump_timer--;
+ if (bump_timer == 0) blockAddLayer();
+}
+
+void blockGravity() {
+ int i,j;
+
+ for (j=0;j<COLS;j++) {
+ for (i=0;i<ROWS-1;i++) {
+ if (blocks[i][j].alive && !blocks[i+1][j].alive)
+ blockSwitch(i,j,i+1,j);
+ }
+ }
+}
+
+void blockMatch() {
+ int i,j,k;
+ int match_count = 0;
+
+ // next, mark all the blocks that will be cleared
+ for (i=0;i<ROWS;i++) {
+ for(j=0;j<COLS;j++) {
+ if (blocks[i][j].alive) {
+ // horizontal matches
+ match_count = 0;
+ for(k=j+1;k<COLS;k++) {
+ if (blockCompare(i,j,i,k))
+ match_count++;
+ else
+ break;
+ }
+ if (match_count > 1) {
+ for(k=j;k<j+match_count+1;k++) {
+ if (!blocks[i][j].matched) clear_delay = CLEAR_TIME;
+ blocks[i][k].matched = true;
+ }
+ }
+ // vertical matches
+ match_count = 0;
+ for(k=i+1;k<ROWS;k++) {
+ if (blockCompare(i,j,k,j))
+ match_count++;
+ else
+ break;
+ }
+ if (match_count > 1) {
+ for(k=i;k<i+match_count+1;k++) {
+ if (!blocks[i][j].matched) clear_delay = CLEAR_TIME;
+ blocks[k][j].matched = true;
+ }
+ }
+ }
+ }
+ }
+
+ if (clear_delay > 0) clear_delay--;
+ if (clear_delay == 0) {
+ // now, clear all the matches
+ for (i=0;i<ROWS;i++) {
+ for(j=0;j<COLS;j++) {
+ if (blocks[i][j].matched) {
+ blockClear(i,j);
+ score += POINTS_PER_BLOCK;
+ // if the stack is close to being bumped,
+ // delay it for a second to make things less jarring
+ if (clear_delay < 60) clear_delay = 60;
+ }
+ }
+ }
+ }
+}
+
+void blockAddLayer() {
+ int i,j;
+ int new_color = -1;
+ int last_color = -1;
+
+ if (clear_delay > 0) return;
+
+ // check if one of the columns is full
+ // if so, set game over state
+ for (j=0;j<COLS;j++) {
+ if (blocks[0][j].alive) {
+ game_over = true;
+ return;
+ }
+ }
+
+ if (cursor_y > 0) cursor_y--;
+
+ for (j=0;j<COLS;j++) {
+ for (i=1;i<ROWS;i++) {
+ blockSwitch(i,j,i-1,j);
+ }
+ }
+
+ for(j=0;j<COLS;j++) {
+ new_color = rand() % 6;
+ while (new_color == last_color || new_color == blocks[ROWS-2][j].color) {
+ new_color = rand() % 6;
+ }
+ last_color = new_color;
+ blockSet(ROWS-1,j,true,new_color);
+ }
+
+ bump_timer = BUMP_TIME;
+}
@@ -0,0 +1,54 @@
+/*
+ FreeBlocks - A simple puzzle game, similar to Tetris Attack
+ Copyright (C) 2012 Justin Jacobs
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef BLOCK_H
+#define BLOCK_H
+
+#include "sys.h"
+
+#define BLOCK_SIZE 32
+#define ROWS 15
+#define COLS 8
+#define CLEAR_TIME 30
+#define BUMP_TIME 600
+
+const int POINTS_PER_BLOCK;
+
+typedef struct Block{
+ int x,y;
+ bool alive;
+ int color;
+ bool matched;
+}Block;
+
+extern Block blocks[15][8];
+int clear_delay;
+int bump_timer;
+
+void blockSet(int i, int j, bool alive, int color);
+void blockClear(int i, int j);
+void blockSwitch(int i, int j, int k, int l);
+bool blockCompare(int i, int j, int k, int l);
+void blockInitAll();
+
+void blockLogic();
+void blockGravity();
+void blockMatch();
+void blockAddLayer();
+
+#endif
@@ -0,0 +1,79 @@
+/*
+ FreeBlocks - A simple puzzle game, similar to Tetris Attack
+ Copyright (C) 2012 Justin Jacobs
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "SDL/SDL_ttf.h"
+#include "SDL/SDL_gfxPrimitives.h"
+
+#include "block.h"
+#include "draw.h"
+#include "sys.h"
+
+void drawEverything() {
+ // Fill the screen with black
+ SDL_FillRect(screen,NULL, 0x1b1b1b);
+
+ drawBlocks();
+ drawCursor();
+ drawInfo();
+}
+
+void drawCursor() {
+ SDL_Rect dest;
+ dest.x = cursor_x*BLOCK_SIZE;
+ dest.y = cursor_y*BLOCK_SIZE;
+ SDL_BlitSurface(surface_cursor,NULL,screen,&dest);
+}
+
+void drawBlocks() {
+ int i,j;
+
+ for(i=0;i<ROWS;i++) {
+ for(j=0;j<COLS;j++) {
+ if(blocks[i][j].alive) {
+ SDL_Rect src,dest;
+
+ if (blocks[i][j].matched) src.x = 6 * BLOCK_SIZE;
+ else src.x = blocks[i][j].color * BLOCK_SIZE;
+ src.w = src.h = BLOCK_SIZE;
+
+ dest.x = blocks[i][j].x;
+ dest.y = blocks[i][j].y;
+
+ SDL_BlitSurface(surface_blocks,&src,screen,&dest);
+ }
+ }
+ }
+}
+
+void drawInfo() {
+ char info[256];
+ SDL_Color color = {217,217,217};
+ SDL_Rect dest;
+ dest.x = 8;
+ dest.y = 488;
+
+ if (game_over) sprintf(info,"Score: %-5d Game Over!",score);
+ else sprintf(info,"Score: %-5d",score);
+
+ text_info = TTF_RenderText_Solid(font,info,color);
+ if(!text_info) return;
+
+ SDL_BlitSurface(text_info,NULL,screen,&dest);
+ SDL_FreeSurface(text_info);
+}
+
Oops, something went wrong.

0 comments on commit 12bbb59

Please sign in to comment.