[libfake437] r351 committed - New function: f437_surface_new_sized().

1 view
Skip to first unread message

libfa...@googlecode.com

unread,
Oct 8, 2011, 5:03:54 AM10/8/11
to libfake43...@googlegroups.com
Revision: 351
Author: endga...@gmail.com
Date: Sat Oct 8 01:59:55 2011
Log: New function: f437_surface_new_sized().
http://code.google.com/p/libfake437/source/detail?r=351

Added:
/trunk/examples/sized.c
Modified:
/trunk/.gitignore
/trunk/Makefile.am
/trunk/include/fake437/surface.h
/trunk/src/surface.c

=======================================
--- /dev/null
+++ /trunk/examples/sized.c Sat Oct 8 01:59:55 2011
@@ -0,0 +1,72 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details. A copy of the
+ * WTFPL version 2 is provided in the source archive, at
+ * example/WTFPL-2 .*/
+#include <fake437.h>
+
+int main(int argc, char* argv[]) {
+ SDL_Surface* screen;
+ F437Surface* f437;
+ SDL_Event ev;
+ int i;
+ int x;
+ int y;
+ int done = 0;
+ const F437Font* font = f437_get_font(F437_FONT_8x14);
+ (void)argc;
+ (void)argv;
+ SDL_Init(SDL_INIT_VIDEO);
+
+ screen = SDL_SetVideoMode(640, 480, 0, 0);
+ f437 = f437_surface_new_sized(screen,
+ font,
+ F437_DEFAULT_WIDTH,
+ F437_DEFAULT_HEIGHT);
+ if (f437 == NULL) abort();
+ f437_surface_fg(f437, f437_get_color(F437_COLOR_BBLUE));
+ f437_surface_bg(f437, f437_get_color(F437_COLOR_CYAN));
+ for (x = 0; x < f437->width; x++) {
+ for (y = 0; y < f437->height; y++) {
+ f437_surface_putc(f437, 178);
+ }
+ }
+
+ f437_surface_fg(f437, f437_get_color(F437_COLOR_BRED));
+ f437_surface_bg(f437, f437_get_color(F437_COLOR_BBLACK));
+ f437_surface_goto_xy(f437, 0, 0);
+ f437_surface_puts(f437,
+ "0123456789012345678901234567890123456789"
+ "0123456789012345678901234567890123456789");
+ f437_surface_fg(f437, f437_get_color(F437_COLOR_BWHITE));
+ f437_surface_bg(f437, f437_get_color(F437_COLOR_BLACK));
+ for (y = 0 ; y < 16 ; y++) {
+ f437_surface_goto_xy(f437, 0, y + 1);
+ for (x = 0 ; x < 16 ; x++) {
+ f437_surface_putc(f437, y*16+x);
+ }
+ }
+
+ f437_surface_goto_xy(f437, 0, 17);
+ f437_surface_fg(f437, f437_get_color(F437_COLOR_BGREEN));
+ f437_surface_bg(f437, f437_get_color(F437_COLOR_BLUE));
+ f437_surface_puts(f437, "Transparent is a color too!");
+
+ f437_surface_goto_xy(f437, 0, 17);
+ f437_surface_fg(f437, f437_get_color(F437_COLOR_BLACK));
+ f437_surface_bg(f437, NULL);
+ for (i = 0 ; i < 27 ; i++) f437_surface_putc(f437, 1);
+
+ SDL_Flip(screen);
+ while (!done) {
+ while (SDL_PollEvent(&ev)) {
+ if ((ev.type == SDL_KEYDOWN) &&
+ (ev.key.keysym.sym == SDLK_ESCAPE)) done = 1;
+ }
+ }
+
+ SDL_Quit();
+ return 0;
+}
=======================================
--- /trunk/.gitignore Wed Nov 25 04:29:14 2009
+++ /trunk/.gitignore Sat Oct 8 01:59:55 2011
@@ -19,7 +19,7 @@
!doc/Makefile.am
!doc/libfake437.texi
examples/example
-examples/guy
+examples/sized
examples/surface
libtool
version.texi
=======================================
--- /trunk/Makefile.am Sat Oct 1 16:01:46 2011
+++ /trunk/Makefile.am Sat Oct 8 01:59:55 2011
@@ -37,11 +37,15 @@
if ENABLE_EXAMPLES
noinst_PROGRAMS = \
examples/example \
+ examples/sized \
examples/surface

examples_example_SOURCES = examples/example.c
examples_example_LDADD = $(SDL_LIBS) libfake437.la

+examples_sized_SOURCES = examples/sized.c
+examples_sized_LDADD = $(SDL_LIBS) libfake437.la
+
examples_surface_SOURCES = examples/surface.c
examples_surface_LDADD = $(SDL_LIBS) libfake437.la
endif
=======================================
--- /trunk/include/fake437/surface.h Sat Oct 1 16:02:41 2011
+++ /trunk/include/fake437/surface.h Sat Oct 8 01:59:55 2011
@@ -76,6 +76,25 @@
const F437Font* font,
int scale);

+/**
+ ** @deftypefun F437Surface* f437_surface_new_sized @
+ ** (SDL_Surface* @var{surface}, @
+ ** const F437Font* @var{font}, @
+ ** int @var{width}, @
+ ** int @var{height})
+ **
+ ** Create a surface of specified width and height. The surface is
+ ** scaled as large as possible and centred. Return @code{NULL} if
+ ** @var{surface} is too small for the requested size, or on
+ ** allocation failure.
+ **
+ ** @end deftypefun
+ **/
+F437Surface* f437_surface_new_sized(SDL_Surface* surface,
+ const F437Font* font,
+ int width,
+ int height);
+
/**
** @deftypefun SDL_Surface* f437_surface_free @
** (F437Surface* @var{surface})
=======================================
--- /trunk/src/surface.c Sat Oct 1 16:02:41 2011
+++ /trunk/src/surface.c Sat Oct 8 01:59:55 2011
@@ -49,6 +49,30 @@
result->fg = result->bg = NULL;
return result;
}
+
+F437Surface* f437_surface_new_sized(SDL_Surface* surface,
+ const F437Font* font,
+ int width,
+ int height) {
+ /* Find the largest suitable scale. */
+ int width_scale = surface->w / font->char_width / width;
+ int height_scale = surface->h / font->char_height / height;
+ int scale = width_scale < height_scale ? width_scale : height_scale;
+ if (scale == 0) return NULL;
+
+ F437Surface* result = f437_surface_try_new(surface, font, scale);
+ if (result == NULL) return NULL;
+
+ /* Now truncate width/height and adjust offsets. */
+ int width_diff = result->width - width;
+ result->left += (width_diff * scale * font->char_width) / 2;
+ result->width = width;
+
+ int height_diff = result->height - height;
+ result->top += (height_diff * scale * font->char_height) / 2;
+ result->height = height;
+ return result;
+}

SDL_Surface* f437_surface_free(F437Surface* surface) {
SDL_Surface* result;

Reply all
Reply to author
Forward
0 new messages