source: abuse/trunk/src/imlib/image.h @ 664

Last change on this file since 664 was 664, checked in by Sam Hocevar, 11 years ago

starting GetClip? refactoring

  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#ifndef _IMAGE_HPP_
12#define _IMAGE_HPP_
13
14#include <stdlib.h>
15#include "linked.h"
16#include "palette.h"
17#include "specs.h"
18#define MAX_DIRTY 200
19
20void image_init();
21void image_uninit();
22extern linked_list image_list;
23
24class dirty_rect : public linked_node
25{
26public :
27    int16_t dx1, dy1, dx2, dy2;
28    dirty_rect(int16_t x1, int16_t y1, int16_t x2, int16_t y2)
29    {
30        dx1 = x1; dy1 = y1; dx2 = x2; dy2 = y2;
31        if (x2 < x1 || y2 < y1)
32            printf("add incorrect dirty\n");
33    }
34    virtual int compare(void *n1) { return ((dirty_rect *)n1)->dy1 > dy1; }
35};
36
37class image_descriptor
38{
39private:
40    vec2i m_size, m_aa, m_bb;
41
42public:
43    uint8_t keep_dirt,
44            static_mem; // if set, don't free memory on exit
45
46    linked_list dirties;
47    void *extended_descriptor;
48
49    image_descriptor(vec2i size, int keep_dirties = 1, int static_memory = 0);
50    int bound_x1(int x1) { return Max(x1, m_aa.x); }
51    int bound_y1(int y1) { return Max(y1, m_aa.y); }
52    int bound_x2(int x2) { return Min(x2, m_bb.x); }
53    int bound_y2(int y2) { return Min(y2, m_bb.y); }
54    inline int x1_clip() { return m_aa.x; }
55    inline int y1_clip() { return m_aa.y; }
56    inline int x2_clip() { return m_bb.x; }
57    inline int y2_clip() { return m_bb.y; }
58    void ClearDirties();
59    void GetClip(int &x1, int &y1, int &x2, int &y2)
60    {
61        x1 = m_aa.x; y1 = m_aa.y; x2 = m_bb.x; y2 = m_bb.y;
62    }
63    void SetClip(int x1, int y1, int x2, int y2)
64    {
65        if(x2 < x1 + 1) x2 = x1 + 1;
66        if(y2 < y1 + 1) y2 = y1 + 1;
67        m_aa.x = Max(x1, 0); m_aa.y = Max(y1, 0);
68        m_bb.x = Min(x2, m_size.x); m_bb.y = Min(y2, m_size.y);
69    }
70    void ReduceDirties();
71    void AddDirty(int x1, int y1, int x2, int y2);
72    void delete_dirty(int x1, int y1, int x2, int y2);
73    void Resize(vec2i size)
74    {
75        m_size = size;
76        m_aa = vec2i(0);
77        m_bb = size;
78    }
79};
80
81class image : public linked_node
82{
83private:
84    uint8_t *m_data;
85    vec2i m_size;
86    bool m_locked;
87
88    void MakePage(vec2i size, uint8_t *page_buffer);
89    void DeletePage();
90
91public:
92    image_descriptor *m_special;
93
94    image(bFILE *fp, spec_entry *e = NULL);
95    image(vec2i size, uint8_t *page_buffer = NULL, int create_descriptor = 0);
96    ~image();
97
98    void Lock();
99    void Unlock();
100
101    uint8_t Pixel(vec2i pos);
102    void PutPixel(vec2i pos, uint8_t color);
103
104    inline uint8_t *scan_line(int16_t y)
105    {
106        return m_data + y * m_size.x;
107    }
108    image *copy(); // makes a copy of an image
109    void clear(int16_t color = -1); // -1 is background color
110
111    vec2i Size() const { return m_size; }
112
113    void scroll(int16_t x1, int16_t y1, int16_t x2, int16_t y2,
114                int16_t xd, int16_t yd);
115    void PutImage(image *screen, vec2i pos, int transparent = 0);
116    void PutPart(image *screen, vec2i pos, vec2i aa, vec2i bb,
117                 int transparent = 0);
118    void PutPartMasked(image *screen, vec2i pos, image *mask, vec2i mpos,
119                       vec2i aa, vec2i bb);
120    image *copy_part_dithered(int16_t x1, int16_t y1, int16_t x2, int16_t y2);
121    void Bar(vec2i p1, vec2i p2, uint8_t color);
122    void xor_bar(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color);
123    void WidgetBar(vec2i p1, vec2i p2,
124                   uint8_t light, uint8_t med, uint8_t dark);
125    void Line(vec2i p1, vec2i p2, uint8_t color);
126    void Rectangle(vec2i p1, vec2i p2, uint8_t color);
127    void burn_led(int16_t x, int16_t y, int32_t num, int16_t color,
128                  int16_t scale = 1);
129    void SetClip(int x1, int y1, int x2, int y2);
130    void GetClip(int &x1, int &y1, int &x2, int &y2);
131    void InClip(int x1, int y1, int x2, int y2);
132
133    void dirt_off()
134    {
135        if(m_special && m_special->keep_dirt) m_special->keep_dirt = 0;
136    }
137    void dirt_on()
138    {
139        if(m_special) m_special->keep_dirt = 1;
140    }
141
142    void AddDirty(int x1, int y1, int x2, int y2)
143    {
144        if (m_special) m_special->AddDirty(x1, y1, x2, y2);
145    }
146    void delete_dirty(int x1, int y1, int x2, int y2)
147    {
148        if(m_special) m_special->delete_dirty(x1, y1, x2, y2);
149    }
150    void ClearDirties()
151    {
152        if (m_special) m_special->ClearDirties();
153    }
154    void dither(palette *pal); // use a b&w palette!
155    void Scale(vec2i size);
156    void SetSize(vec2i size, uint8_t *page = NULL);
157    void flood_fill(int16_t x, int16_t y, uint8_t color);
158    image *create_smooth(int16_t smoothness = 1); // 0 no smoothness
159    void unpack_scanline(int16_t line, char bitsperpixel = 1);
160    void FlipX();
161    void FlipY();
162};
163
164class image_controller
165{
166public:
167    image_controller()
168    {
169        image_init();
170    }
171    ~image_controller()
172    {
173        image_uninit();
174    }
175};
176
177#endif /* _IMAGE_HPP_ */
178
Note: See TracBrowser for help on using the repository browser.