summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pjm@nac.net>2013-02-18 17:57:14 (EST)
committer P. J. McDermott <pjm@nac.net>2013-02-18 17:57:14 (EST)
commite289e1dd67d2e1b7769bb0fdd23d064a6c596b2b (patch)
treea211c8f1343fdb831cff96e4de74b3948847a08b
parentc250247b7be8b6a0942f7aaeaab95ad6fef933be (diff)
downloadoverworld-rpg-e289e1dd67d2e1b7769bb0fdd23d064a6c596b2b.zip
overworld-rpg-e289e1dd67d2e1b7769bb0fdd23d064a6c596b2b.tar.gz
overworld-rpg-e289e1dd67d2e1b7769bb0fdd23d064a6c596b2b.tar.bz2
Begin work on areas and viewports.
-rw-r--r--src/Makefile.am2
-rw-r--r--src/area.c50
-rw-r--r--src/area.h16
-rw-r--r--src/init.c6
-rw-r--r--src/init.h2
-rw-r--r--src/layer.h13
-rw-r--r--src/main.c3
-rw-r--r--src/viewport.c28
-rw-r--r--src/viewport.h16
9 files changed, 128 insertions, 8 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index a117d1e..2f349b4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -7,4 +7,6 @@ src_SOURCES = \
src/xml.c \
src/base64.c \
src/compression.c \
+ src/viewport.c \
+ src/area.c \
$(src_resources_SOURCES)
diff --git a/src/area.c b/src/area.c
new file mode 100644
index 0000000..3a03198
--- /dev/null
+++ b/src/area.c
@@ -0,0 +1,50 @@
+#include <SDL.h>
+#include "area.h"
+#include "layer.h"
+#include "viewport.h"
+
+void
+render_area_to_viewport(struct area *area, struct viewport *vp)
+{
+ SDL_Rect srcrect;
+ SDL_Rect dstrect;
+
+ srcrect.x = vp->x;
+ srcrect.y = vp->y;
+ srcrect.w = vp->w;
+ srcrect.h = vp->h;
+ dstrect.x = 0;
+ dstrect.y = 0;
+ dstrect.w = vp->w;
+ dstrect.h = vp->h;
+
+ render_area_to_surface(area, &srcrect, vp->screen, &dstrect);
+}
+
+void
+render_area_to_surface(struct area *area, SDL_Rect *arearect,
+ SDL_Surface *dst, SDL_Rect *dstrect)
+{
+ /* Fast fill with black to avoid artifacts in off-map pixels. */
+ SDL_FillRect(dst, dstrect, 0);
+
+ /* Blit ground layer. */
+ SDL_BlitSurface(area->map_layers[LAYER_GROUND], arearect,
+ dst, dstrect);
+
+ /* Blit low objects layer. */
+ SDL_BlitSurface(area->map_layers[LAYER_OBJ_LOW], arearect,
+ dst, dstrect);
+
+ /* TODO: Blit low sprites. */
+
+ /* Blit middle objects layer. */
+ SDL_BlitSurface(area->map_layers[LAYER_OBJ_MID], arearect,
+ dst, dstrect);
+
+ /* TODO: Blit high sprites. */
+
+ /* Blit high objects layer. */
+ SDL_BlitSurface(area->map_layers[LAYER_OBJ_HIGH], arearect,
+ dst, dstrect);
+}
diff --git a/src/area.h b/src/area.h
new file mode 100644
index 0000000..644ab16
--- /dev/null
+++ b/src/area.h
@@ -0,0 +1,16 @@
+#ifndef AREA_H
+#define AREA_H
+
+#include <SDL.h>
+#include "layer.h"
+#include "viewport.h"
+
+struct area {
+ SDL_Surface *map_layers[LAYERS_MAX];
+};
+
+void render_area_to_viewport(struct area *area, struct viewport *vp);
+void render_area_to_surface(struct area *area, SDL_Rect *arearect,
+ SDL_Surface *dst, SDL_Rect *dstrect);
+
+#endif
diff --git a/src/init.c b/src/init.c
index f893d1d..921ce6e 100644
--- a/src/init.c
+++ b/src/init.c
@@ -15,12 +15,6 @@ init(void)
if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) {
err(1, "Failed to initialize SDL_Image (%s)", IMG_GetError());
}
-
- debug("Setting video mode...");
- screen = SDL_SetVideoMode(240, 160, 8, SDL_SWSURFACE | SDL_ANYFORMAT);
- if (screen == NULL) {
- err(1, "Failed to set video mode (%s)", SDL_GetError());
- }
}
void
diff --git a/src/init.h b/src/init.h
index f04b1fe..6f13c65 100644
--- a/src/init.h
+++ b/src/init.h
@@ -3,8 +3,6 @@
#include <SDL.h>
-SDL_Surface *screen;
-
void init(void);
void quit(int status);
diff --git a/src/layer.h b/src/layer.h
new file mode 100644
index 0000000..09ad462
--- /dev/null
+++ b/src/layer.h
@@ -0,0 +1,13 @@
+#ifndef LAYER_H
+#define LAYER_H
+
+enum layer_id {
+ LAYER_GROUND = 0,
+ LAYER_OBJ_LOW,
+ LAYER_OBJ_MID,
+ LAYER_OBJ_HIGH,
+ LAYER_WEATHER,
+ LAYERS_MAX
+};
+
+#endif
diff --git a/src/main.c b/src/main.c
index dc85590..b3ad78c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,10 +3,12 @@
#include "logging.h"
#include "resources/image.h"
#include "resources/map.h"
+#include "viewport.h"
int
main(void)
{
+ SDL_Surface *screen;
struct map *map;
struct image *img;
struct map_tileset *ts;
@@ -15,6 +17,7 @@ main(void)
SDL_Rect imgrect, surfacerect;
init();
+ screen = init_viewport(240, 160, 8)->screen;
map = map_get("data/forest1.tmx");
debug("Map dimensions: %dx%d", map->width, map->height);
diff --git a/src/viewport.c b/src/viewport.c
new file mode 100644
index 0000000..97e73dd
--- /dev/null
+++ b/src/viewport.c
@@ -0,0 +1,28 @@
+#include <SDL.h>
+#include "viewport.h"
+#include "logging.h"
+
+struct viewport *
+init_viewport(int width, int height, int bpp)
+{
+ struct viewport *vp;
+
+ vp = malloc(sizeof(*vp));
+ if (vp == NULL) {
+ err(1, "Failed to allocate viewport");
+ }
+
+ vp->x = 0;
+ vp->y = 0;
+ vp->w= width;
+ vp->h= height;
+
+ debug("Setting video mode...");
+ vp->screen = SDL_SetVideoMode(width, height, bpp,
+ SDL_SWSURFACE | SDL_ANYFORMAT);
+ if (vp->screen == NULL) {
+ err(1, "Failed to set video mode (%s)", SDL_GetError());
+ }
+
+ return vp;
+}
diff --git a/src/viewport.h b/src/viewport.h
new file mode 100644
index 0000000..372a285
--- /dev/null
+++ b/src/viewport.h
@@ -0,0 +1,16 @@
+#ifndef VIEWPORT_H
+#define VIEWPORT_H
+
+#include <SDL.h>
+
+struct viewport {
+ int x;
+ int y;
+ int w;
+ int h;
+ SDL_Surface *screen;
+};
+
+struct viewport *init_viewport(int width, int height, int bpp);
+
+#endif