Modularization started.

This commit is contained in:
Murilo Soares Pereira 2010-04-01 02:28:00 -03:00
parent d97099fe4d
commit 4a84d62d07
6 changed files with 159 additions and 144 deletions

44
lib/card.c Normal file
View File

@ -0,0 +1,44 @@
#include <ncurses.h>
#include <malloc.h>
#include "card.h"
#include "frame.h"
#include "../src/common.h"
struct card *initialize_card() {
struct card *card = NULL;
card = malloc(sizeof(card));
card->frame = initialize_frame();
card->value = NONE;
card->suit = NONE;
card->exposed = FALSE;
return(card);
}
void delete_card(struct card *card) {
delete_frame(card->frame);
free(card);
return;
}
void set_card(struct card *card,
enum value value,
enum suit suit,
char exposed,
int start_y,
int start_x) {
set_frame(card->frame, start_y, start_x);
card->value = value;
card->suit = suit;
card->exposed = exposed;
return;
}
void refresh_card(struct card *card) {
box(card->frame->shape, 0, 0);
wrefresh(card->frame->shape);
}

41
lib/card.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef CARD_H
#define CARD_H
enum suit {
BLANK = -1,
DIAMONDS = 0,
SPADES = 1,
HEARTS = 2,
CLUBS = 3
};
enum value {
NONE = -1,
TWO = 2,
THREE = 3,
FOUR = 4,
FIVE = 5,
SIX = 6,
SEVEN = 7,
EIGHT = 8,
NINE = 9,
TEN = 10,
JACK = 11,
QUEEN = 12,
KING = 13,
ACE = 14
};
struct card {
struct frame *frame;
enum value value;
enum suit suit;
char exposed;
};
struct card *initialize_card();
void delete_card(struct card *);
void set_card(struct card *, enum value, enum suit, char, int, int);
void refresh_card(struct card *);
#endif

43
lib/frame.c Normal file
View File

@ -0,0 +1,43 @@
#include <ncurses.h>
#include <malloc.h>
#include "frame.h"
WINDOW *initialize_shape() {
WINDOW *shape;
shape = malloc(sizeof(shape));
return(shape);
}
struct frame *initialize_frame() {
struct frame *frame = NULL;
frame = malloc(sizeof(frame));
frame->shape = initialize_shape();
frame->height = FRAME_HEIGHT;
frame->width = FRAME_WIDTH;
frame->start_y = 0;
frame->start_x = 0;
return(frame);
}
void delete_frame(struct frame *frame) {
free(frame->shape);
free(frame);
return;
}
void set_frame(struct frame *frame, int start_y, int start_x) {
frame->start_y = start_y;
frame->start_x = start_x;
frame->shape = newwin(frame->height,
frame->width,
frame->start_y,
frame->start_x);
return;
}

20
lib/frame.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef FRAME_H
#define FRAME_H
#define FRAME_WIDTH 7
#define FRAME_HEIGHT 5
struct frame {
WINDOW *shape;
int height;
int width;
int start_y;
int start_x;
};
WINDOW *initialize_shape();
struct frame *initialize_frame();
void delete_frame(struct frame *);
void set_frame(struct frame *, int, int);
#endif

View File

@ -3,10 +3,12 @@ SRC_DIR = src
SRC = ${SRC_DIR}/tty-solitaire.c SRC = ${SRC_DIR}/tty-solitaire.c
CFLAGS = -W -Wall -pedantic -ansi -std=c99 CFLAGS = -W -Wall -pedantic -ansi -std=c99
OUTPUT = bin/tty-solitaire OUTPUT = bin/tty-solitaire
LIB_DIR = lib
OBJECTS = ${LIB_DIR}/card.o ${LIB_DIR}/frame.o
LDFLAGS = -lncurses LDFLAGS = -lncurses
tty-solitaire: ${SRC} tty-solitaire: ${OBJECTS}
${CC} ${CFLAGS} ${LDFLAGS} ${SRC} -o ${OUTPUT} ${CC} ${CFLAGS} ${LDFLAGS} ${SRC} -o ${OUTPUT} ${OBJECTS}
clean: clean:
rm -rf ${SRC_DIR}/*.o ${OUTPUT} rm -rf ${LIB_DIR}/*.o ${OUTPUT}

View File

@ -2,58 +2,7 @@
#include <malloc.h> #include <malloc.h>
#include <string.h> #include <string.h>
#include "common.h" #include "common.h"
#include "../lib/card.h"
#define CARD_FRAME_WIDTH 7
#define CARD_FRAME_HEIGHT 5
enum suit {
BLANK = -1,
DIAMONDS = 0,
SPADES = 1,
HEARTS = 2,
CLUBS = 3
};
enum value {
NONE = -1,
TWO = 2,
THREE = 3,
FOUR = 4,
FIVE = 5,
SIX = 6,
SEVEN = 7,
EIGHT = 8,
NINE = 9,
TEN = 10,
JACK = 11,
QUEEN = 12,
KING = 13,
ACE = 14
};
struct card_frame {
WINDOW *shape;
int height;
int width;
int start_y;
int start_x;
};
struct card {
struct card_frame *frame;
enum value value;
enum suit suit;
char exposed;
};
WINDOW *initialize_shape();
struct card_frame *initialize_card_frame();
struct card *initialize_card();
void draw_card(struct card *);
void set_card_frame(struct card_frame *, int, int);
void set_card_attributes(struct card *, enum value, enum suit, char);
void set_card(struct card *, enum value, enum suit, char, int, int);
void delete_card(struct card *);
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {
char message[] = "Welcome to tty-solitaire."; char message[] = "Welcome to tty-solitaire.";
@ -76,25 +25,25 @@ int main(int argc, const char *argv[]) {
set_card(card, KING, CLUBS, TRUE, 1, 1); set_card(card, KING, CLUBS, TRUE, 1, 1);
draw_card(card); refresh_card(card);
while ((ch = getch()) != KEY_F(1)) { while ((ch = getch()) != KEY_F(1)) {
switch(ch) { switch(ch) {
case KEY_LEFT: case KEY_LEFT:
case 'h': case 'h':
draw_card(card); refresh_card(card);
break; break;
case KEY_RIGHT: case KEY_RIGHT:
case 'l': case 'l':
draw_card(card); refresh_card(card);
break; break;
case KEY_UP: case KEY_UP:
case 'k': case 'k':
draw_card(card); refresh_card(card);
break; break;
case KEY_DOWN: case KEY_DOWN:
case 'j': case 'j':
draw_card(card); refresh_card(card);
break; break;
} }
} }
@ -104,87 +53,3 @@ int main(int argc, const char *argv[]) {
return 0; return 0;
} }
WINDOW *initialize_shape() {
WINDOW *shape;
shape = malloc(sizeof(shape));
return(shape);
}
struct card_frame *initialize_card_frame() {
struct card_frame *card_frame = NULL;
card_frame = malloc(sizeof(card_frame));
card_frame->shape = initialize_shape();
card_frame->height = CARD_FRAME_HEIGHT;
card_frame->width = CARD_FRAME_WIDTH;
card_frame->start_y = 0;
card_frame->start_x = 0;
return(card_frame);
}
struct card *initialize_card() {
struct card *card = NULL;
card = malloc(sizeof(card));
card->frame = initialize_card_frame();
card->value = NONE;
card->suit = NONE;
card->exposed = FALSE;
return(card);
}
void draw_card(struct card *card) {
box(card->frame->shape, 0, 0);
wrefresh(card->frame->shape);
}
void set_card_frame(struct card_frame *card_frame,
int start_y,
int start_x) {
card_frame->start_y = start_y;
card_frame->start_x = start_x;
card_frame->shape = newwin(card_frame->height,
card_frame->width,
card_frame->start_y,
card_frame->start_x);
return;
}
void set_card_attributes(struct card *card,
enum value value,
enum suit suit,
char exposed) {
card->value = value;
card->suit = suit;
card->exposed = exposed;
return;
}
void set_card(struct card *card,
enum value value,
enum suit suit,
char exposed,
int start_y,
int start_x) {
set_card_attributes(card, value, suit, exposed);
set_card_frame(card->frame, start_y, start_x);
return;
}
void delete_card(struct card *card) {
free(card->frame->shape);
free(card->frame);
free(card);
return;
}