Skip to content

Commit

Permalink
feat: 0.2.8 add display_sprite_at_coords()
Browse files Browse the repository at this point in the history
Bumps file format version to 0.1.4, with correct spacing after '=' for
array declaration.
  • Loading branch information
jgabaut committed Aug 31, 2023
1 parent 97135db commit 4ff9170
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 23 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export SHELL=/bin/bash

VERSION= v0.2.7
VERSION= v0.2.8
FLAGS = -Werror -Wall -Wpedantic -Wfatal-errors

all: demo
Expand Down Expand Up @@ -63,4 +63,6 @@ debug:
gcc .demo.o .animate.o -o demo_debug -lncurses
@echo -e "Done.\nUse gdb to debug the executable.\n"

rebuild: clean all

$(V).SILENT:
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ This is a C library offering some functions to display an animation read from a
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
C 1 44 130 203
C/C++ Header 1 10 5 61
C 1 49 153 233
C/C++ Header 1 20 5 68
-------------------------------------------------------------------------------
SUM: 2 54 135 264
SUM: 2 69 158 301
-------------------------------------------------------------------------------
```

Expand Down
4 changes: 2 additions & 2 deletions demofile.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
0.1.3
char sprites[31][18][18] ={
0.1.4
char sprites[31][18][18] = {

//Frame 1, file chest-animation/image1.png
{
Expand Down
2 changes: 1 addition & 1 deletion documentation/s4c.doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ PROJECT_NAME = "sprites4curses"
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = "0.2.7"
PROJECT_NUMBER = "0.2.8"

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down
52 changes: 45 additions & 7 deletions s4c-animate/animate.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ int load_sprites(char sprites[MAXFRAMES][MAXROWS][MAXCOLS], FILE* f, int rows, i
char line[1024];
char* file_version;
char* token;
char* READER_VERSION = "0.1.3";
char* READER_VERSION = "0.1.4";
int row = 0, frame = -1;

int check = -1;
Expand Down Expand Up @@ -199,8 +199,7 @@ int animate_sprites(char sprites[MAXFRAMES][MAXROWS][MAXCOLS], WINDOW* w, int re

/**
* Takes a WINDOW pointer to print into and a string for the file passed.
* Loads sprites from the file and displays them in the passed window if it is big enough.
* File format should have a sprite line on each line, or be a valid array definition.
* Takes a pre-initialised array of sprites, valid format output by sprites.py or sheet_converter.py.
* Color-character map is define in print_spriteline().
* Sets all the frames to the passed array.
* @see print_spriteline()
Expand Down Expand Up @@ -319,12 +318,10 @@ void *animate_sprites_thread_at(void *args_ptr) {
}

/**
* Takes a WINDOW pointer to print into and a string for the file passed.
* Takes a WINDOW pointer to print into.
* Contrary to other of these functions, this one does not touch cursor settings.
* Loads sprites from the file and displays a range of them in the passed window if it is big enough.
* File format should have a sprite line on each line, or be a valid array definition.
* Uses the passed sprites and displays a range of them in the passed window if it is big enough.
* Color-character map is define in print_spriteline().
* Sets all the frames to the passed array.
* @see print_spriteline()
* @param sprites The sprites array.
* @param w The window to print into.
Expand Down Expand Up @@ -377,6 +374,47 @@ int animate_rangeof_sprites_at_coords(char sprites[MAXFRAMES][MAXROWS][MAXCOLS],
return 1;
}

/**
* Takes a WINDOW pointer to print into and an animation array, plus the index of requested frame to print.
* Contrary to other of these functions, this one does not touch cursor settings.
* Color-character map is define in print_spriteline().
* Sets all the frames to the passed array.
* @see print_spriteline()
* @param sprites The sprites array.
* @param sprite_index The index of requested sprite.
* @param w The window to print into.
* @param num_frames How many frames the animation will have.
* @param frameheight Height of the frame.
* @param framewidth Width of the frame.
* @param startY Y coord of the window to start printing to.
* @param startY X coord of the window to start printing to.
* @see S4C_ERR_SMALL_WIN
* @return 1 if successful, a negative value for errors.
*/
int display_sprite_at_coords(char sprites[MAXFRAMES][MAXROWS][MAXCOLS], int sprite_index, WINDOW* w, int num_frames, int frameheight, int framewidth, int startX, int startY) {
//Validate requested range
if (sprite_index < 0 || sprite_index > num_frames ) {
return S4C_ERR_RANGE;
}

int rows = frameheight;
int cols = framewidth;

// Check if window is big enough
int win_rows, win_cols;
getmaxyx(w, win_rows, win_cols);
if (win_rows < rows + startY || win_cols < cols + startX) {
return S4C_ERR_SMALL_WIN; //fprintf(stderr, "animate => Window is too small to display the sprite.\n");
}
for (int j=0; j<rows; j++) {
// Print current frame
print_spriteline(w,sprites[sprite_index][j], j+startY+1, cols, startX);
}
box(w,0,0);
wrefresh(w);
return 1;
}

/**
* Takes a source animation vector matrix and a destination to copy to.
* @param source The source sprites array.
Expand Down
6 changes: 4 additions & 2 deletions s4c-animate/animate.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#include <ncurses.h>
#include <pthread.h>

#define S4C_ANIMATE_VERSION "0.2.7"
#define S4C_ANIMATE_VERSION "0.2.8"
#define S4C_ANIMATE_MAJOR_VERSION 0
#define S4C_ANIMATE_MINOR_VERSION 2
#define S4C_ANIMATE_PATCH_VERSION 7
#define S4C_ANIMATE_PATCH_VERSION 8

void s4c_printVersionToFile(FILE* f);
void s4c_echoVersionToFile(FILE* f);
Expand Down Expand Up @@ -87,5 +87,7 @@ int animate_sprites_at_coords(char sprites[MAXFRAMES][MAXROWS][MAXCOLS], WINDOW*

int animate_rangeof_sprites_at_coords(char sprites[MAXFRAMES][MAXROWS][MAXCOLS], WINDOW* w, int fromFrame, int toFrame, int repetitions, int frametime, int num_frames, int frameheight, int framewidth, int startX, int startY);

int display_sprite_at_coords(char sprites[MAXFRAMES][MAXROWS][MAXCOLS], int sprite_index, WINDOW* w, int num_frames, int frameheight, int framewidth, int startX, int startY);

void copy_animation(char source[MAXFRAMES][MAXROWS][MAXCOLS], char dest[MAXFRAMES][MAXROWS][MAXCOLS]);
#endif
36 changes: 35 additions & 1 deletion s4c-demo/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,40 @@ int demo(FILE* mainthread_file, FILE* newthread_file) {
exit(EXIT_FAILURE);
}

// We clear the window, and expect animate__() to refresh it
wclear(w);
wrefresh(w);
wclear(stdscr);
wrefresh(stdscr);
mvwprintw(stdscr,3,2, "Now display_sprite_at_coords, using frame index 13:");
mvwprintw(stdscr,4,2, "This function puts the requested frame of UL animation corner at (y:%i,x:%i).", try_y, try_x);
mvwprintw(stdscr,6,20, "[Press Enter to continue]");
wrefresh(stdscr);
scanf("%*c");
wclear(stdscr);
wrefresh(stdscr);

// We call the animation to be displayed at 3,3
result = display_sprite_at_coords(sprites, 13, w, num_frames, frame_height, frame_width, try_y, try_x);
// We should check animate_sprites_at_coords() result to see if there were problems, but in the demo we don't expect problems so we ignore the specific error content of result and just exit.

if (result < 0) {
endwin();
fprintf(stderr,"Demo error while doing display_sprite_at_coords()");
exit(EXIT_FAILURE);
}

mvwprintw(stdscr,6,20, "[Press Enter to continue]");
wrefresh(stdscr);
scanf("%*c");
wclear(stdscr);
wrefresh(stdscr);

wclear(w);
wrefresh(w);

wclear(stdscr);
wrefresh(stdscr);

// We clear the window, and expect animate__() to refresh it
wclear(w);
Expand Down Expand Up @@ -260,10 +294,10 @@ int demo(FILE* mainthread_file, FILE* newthread_file) {

//Free animation thread arguments and end demo
free(animation_thread_args);
endwin();

clear();
refresh();
endwin();
printf("\n\n\t\tEnd of demo.");
printf("\n\t\t[Press Enter to end the demo]\n");
scanf("%*c");
Expand Down
6 changes: 3 additions & 3 deletions scripts/sheet_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#
# @section author_spritesheet Author(s)
# - Created by jgabaut on 24/02/2023.
# - Modified by jgabaut on 23/05/2023.
# - Modified by jgabaut on 31/08/2023.

# Imports
from PIL import Image
Expand All @@ -44,7 +44,7 @@
import math

## The file format version.
FILE_VERSION = "0.1.3"
FILE_VERSION = "0.1.4"

# Functions
def usage():
Expand Down Expand Up @@ -134,7 +134,7 @@ def convert_spritesheet(filename, spriteSizeX, spriteSizeY, separatorSize, start
# Start file output, beginning with version number

print("{}".format(FILE_VERSION))
print("char sprites[{}][{}][{}] =".format(len(sprites) +1, spriteSizeY+1, spriteSizeX+1) + "{\n")
print("char sprites[{}][{}][{}] = ".format(len(sprites) +1, spriteSizeY+1, spriteSizeX+1) + "{\n")
for i, sprite in enumerate(sprites):
print("\t//Sprite {}, index {}".format(i + 1, i))
print("\t{")
Expand Down
6 changes: 3 additions & 3 deletions scripts/sprites.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#
# @section author_sprites Author(s)
# - Created by jgabaut on 24/02/2023.
# - Modified by jgabaut on 23/05/2023.
# - Modified by jgabaut on 31/08/2023.

# Imports
from PIL import Image
Expand All @@ -43,7 +43,7 @@
import math

## The file format version.
FILE_VERSION = "0.1.3"
FILE_VERSION = "0.1.4"

# Expects the sprite directory name as first argument.
# File names format inside the directory should be "imageNUM.png".
Expand Down Expand Up @@ -133,7 +133,7 @@ def print_converted_sprites(direc):
# Start file output, beginning with version number

print("{}".format(FILE_VERSION))
print("char sprites[{}][18][18] =".format(frames) + "{\n")
print("char sprites[{}][18][18] = ".format(frames) + "{\n")
idx = 1
for file in sorted(glob.glob('{}/*.png'.format(direc)),
key=lambda f:
Expand Down

0 comments on commit 4ff9170

Please sign in to comment.