From 3e2e4fe9e05d73ba1c1b5de93160bb1cdb7cb56e Mon Sep 17 00:00:00 2001 From: pixel Date: Fri, 28 Mar 2003 13:36:38 +0000 Subject: SDL backend mostly finished --- lib/font.cc | 40 +++++++------- lib/glshape.cc | 27 +++++++++- lib/glsprite.cc | 8 +-- lib/mcolor.cc | 8 +++ lib/shape.cc | 161 ++++++++++++++++---------------------------------------- lib/sprite.cc | 41 ++++++++------- 6 files changed, 124 insertions(+), 161 deletions(-) (limited to 'lib') diff --git a/lib/font.cc b/lib/font.cc index 9e77ae2..93588bf 100644 --- a/lib/font.cc +++ b/lib/font.cc @@ -1,10 +1,12 @@ #include +#include +#include #include "base.h" #include "font.h" -#include "Input.h" #ifdef HAVE_CONFIG_H #include "config.h" #endif +#include "engine.h" Uint8 prescale2[4] = { 0, 85, 170, 255 }, prescale3[8] = { 0, 36, 72, 109, 145, 182, 218, 255 }; @@ -163,16 +165,16 @@ mogltk::font::~font() { } void mogltk::font::drawentry(Uint16 entry, int x, int y, ColorP c) { - bool was2D; + bool locked = false; int trueentry, cx, cy, px, py; + SDL_Rect src, dst; - return; -#if 0 - was2D = mogltk::glbase::is2D(); - - if (!was2D) - mogltk::glbase::Enter2DMode(); - + if (SDL_MUSTLOCK(mogltk::engine::base_o->getsurface())) { + locked = true; + SDL_LockSurface(mogltk::engine::base_o->getsurface()); + } + +#if 0 if (shadow) { int os = shadow; shadow = 0; @@ -181,27 +183,23 @@ void mogltk::font::drawentry(Uint16 entry, int x, int y, ColorP c) { shadow = os; } - +#endif + y -= base; - Bind(entry / nbcT); - c.Bind(); trueentry = entry % nbcT; cx = trueentry % nbcU; cy = trueentry / nbcU; px = cx * maxX; py = cy * maxY; - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2i(px , py ); glVertex2i(x , y ); - glTexCoord2i(px + maxX - 1, py ); glVertex2i(x + maxX - 1, y ); - glTexCoord2i(px , py + maxY - 1); glVertex2i(x , y + maxY - 1); - glTexCoord2i(px + maxX - 1, py + maxY - 1); glVertex2i(x + maxX - 1, y + maxY - 1); - glEnd(); + src.x = px; src.y = py; src.w = maxX; src.h = maxY; + dst.x = x; dst.y = y; dst.w = maxX; dst.h = maxY; - if (!was2D) - mogltk::glbase::Leave2DMode(); -#endif + SDL_BlitSurface(fonttex[entry / nbcT]->GetSurface(), &src, mogltk::engine::base_o->getsurface(), &dst); + + if (locked) + SDL_UnlockSurface(mogltk::engine::base_o->getsurface()); } void mogltk::font::putcursor(int x, int y) { diff --git a/lib/glshape.cc b/lib/glshape.cc index c38d062..0fdaef0 100644 --- a/lib/glshape.cc +++ b/lib/glshape.cc @@ -36,8 +36,31 @@ void mogltk::glshape::box(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, LEAVE; } +void mogltk::glshape::hline(int x1, int x2, int y, ColorP c1, ColorP c2) { + box(x1, y, x2, y, c1, c2, c1, c2); +} + +void mogltk::glshape::vline(int x, int y1, int y2, ColorP c1, ColorP c2) { + box(x, y1, x, y2, c1, c1, c2, c2); +} + +void mogltk::glshape::pixel(int x, int y, ColorP c) { + box(x, y, x, y, c); +} + +void mogltk::glshape::obox(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4) { + ENTER; + + hline(x1, x2, y1, c1, c2); + hline(x1, x2, y2, c3, c4); + vline(x1, y1, y2, c1, c3); + vline(x2, y1, y2, c2, c4); + + LEAVE; +} + void mogltk::glshape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, int tx, int ty, double f, ColorP c) { - shape::tbox(t, x1, y1, x2, y2, tx, ty, f, c); + tbox(t, x1, y1, x2, y2, tx, ty, tx + (int) ((x2 - x1) * f), ty + (int) ((y2 - y1) * f), c); } void mogltk::glshape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, int tx1, int ty1, int tx2, int ty2, ColorP c) { ENTERT; @@ -55,7 +78,7 @@ void mogltk::glshape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, } void mogltk::glshape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4, int tx, int ty, double f) { - shape::tbox(t, x1, y1, x2, y2, c1, c2, c3, c4, tx, ty, f); + tbox(t, x1, y1, x2, y2, c1, c2, c3, c4, tx, ty, tx + (int) ((x2 - x1) * f), ty + (int) ((y2 - y1) * f)); } void mogltk::glshape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4, int tx1, int ty1, int tx2, int ty2) { diff --git a/lib/glsprite.cc b/lib/glsprite.cc index 68350c9..01faada 100644 --- a/lib/glsprite.cc +++ b/lib/glsprite.cc @@ -26,10 +26,10 @@ void mogltk::glSprite::draw(int dx, int dy, ColorP c) { Bind(); glBegin(GL_TRIANGLE_STRIP); - glTexCoord2i(GetPX() , GetPY() ); glVertex2i(dx , dy ); - glTexCoord2i(GetPX() + GetSX() - 1, GetPY() ); glVertex2i(dx + GetSX() - 1, dy ); - glTexCoord2i(GetPX() , GetPY() + GetSY() - 1); glVertex2i(dx , dy + GetSY() - 1); - glTexCoord2i(GetPX() + GetSX() - 1, GetPY() + GetSY() - 1); glVertex2i(dx + GetSX() - 1, dy + GetSY() - 1); + glTexCoord2i(GetPX() , GetPY() ); glVertex2i(dx , dy ); + glTexCoord2i(GetPX() + GetSX(), GetPY() ); glVertex2i(dx + GetSX(), dy ); + glTexCoord2i(GetPX() , GetPY() + GetSY()); glVertex2i(dx , dy + GetSY()); + glTexCoord2i(GetPX() + GetSX(), GetPY() + GetSY()); glVertex2i(dx + GetSX(), dy + GetSY()); glEnd(); if (!was2D) diff --git a/lib/mcolor.cc b/lib/mcolor.cc index 04e0467..806a812 100644 --- a/lib/mcolor.cc +++ b/lib/mcolor.cc @@ -1,5 +1,6 @@ #include #include +#include "engine.h" #include "mcolor.h" Color mogltk::ColorP::Min(0, 0, 0, 0), mogltk::ColorP::Max = WHITE; @@ -14,3 +15,10 @@ void mogltk::ColorP::Bind() { glColor4d((double) MIN(MAX(c.R, Min.R), Max.R) / 255, (double) MIN(MAX(c.G, Min.G), Max.G) / 255, (double) MIN(MAX(c.B, Min.B), Max.B) / 255, (double) MIN(MAX(c.A, Min.A), Max.A) / 255); } +Uint32 mogltk::ColorP::toSDL(SDL_PixelFormat * f) { + if (!f) { + f = mogltk::engine::base_o->getsurface()->format; + } + + return SDL_MapRGBA(f, MIN(MAX(c.R, Min.R), Max.R), MIN(MAX(c.G, Min.G), Max.G), MIN(MAX(c.B, Min.B), Max.B), MIN(MAX(c.A, Min.A), Max.A)); +} diff --git a/lib/shape.cc b/lib/shape.cc index 9386e41..e586d6d 100644 --- a/lib/shape.cc +++ b/lib/shape.cc @@ -9,34 +9,14 @@ #define LEAVE Leave(flag) void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c) { -#if 0 ENTER; - c.Bind(); - glBegin(GL_TRIANGLE_STRIP); - glVertex2i(x1, y1); - glVertex2i(x2 + 1, y1); - glVertex2i(x1, y2 + 1); - glVertex2i(x2 + 1, y2 + 1); - glEnd(); + SDL_Rect rect; - LEAVE; -#endif -} - -void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4) { -#if 0 - ENTER; - - glBegin(GL_TRIANGLE_STRIP); - c1.Bind(); glVertex2i(x1, y1); - c2.Bind(); glVertex2i(x2 + 1, y1); - c3.Bind(); glVertex2i(x1, y2 + 1); - c4.Bind(); glVertex2i(x2 + 1, y2 + 1); - glEnd(); + rect.x = x1; rect.y = y1; rect.w = x2 - x1 + 1; rect.h = y2 - y1 + 1; + SDL_FillRect(mogltk::engine::base_o->getsurface(), &rect, c.toSDL()); LEAVE; -#endif } void mogltk::shape::hline(int x1, int x2, int y, ColorP c) { @@ -75,16 +55,41 @@ void mogltk::shape::vline3d(int x, int y1, int y2, ColorP shade1, ColorP shade2, LEAVE; } -void mogltk::shape::hline(int x1, int x2, int y, ColorP c1, ColorP c2) { - box(x1, y, x2, y, c1, c2, c1, c2); -} - -void mogltk::shape::vline(int x, int y1, int y2, ColorP c1, ColorP c2) { - box(x, y1, x, y2, c1, c1, c2, c2); -} - void mogltk::shape::pixel(int x, int y, ColorP c) { - box(x, y, x, y, c); + ENTER; + + SDL_Surface * surface = mogltk::engine::base_o->getsurface(); + int bpp = surface->format->BytesPerPixel; + Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; + Uint32 pixel = c.toSDL(); + + switch(bpp) { + case 1: + *p = pixel; + break; + + case 2: + *(Uint16 *)p = pixel; + break; + + case 3: + if(SDL_BYTEORDER == SDL_BIG_ENDIAN) { + p[0] = (pixel >> 16) & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = pixel & 0xff; + } else { + p[0] = pixel & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = (pixel >> 16) & 0xff; + } + break; + + case 4: + *(Uint32 *)p = pixel; + break; + } + + LEAVE; } void mogltk::shape::obox(int x1, int y1, int x2, int y2, ColorP c) { @@ -98,98 +103,24 @@ void mogltk::shape::obox(int x1, int y1, int x2, int y2, ColorP c) { LEAVE; } -void mogltk::shape::obox(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4) { - ENTER; - - hline(x1, x2, y1, c1, c2); - hline(x1, x2, y2, c3, c4); - vline(x1, y1, y2, c1, c3); - vline(x2, y1, y2, c2, c4); - - LEAVE; -} - -void mogltk::shape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, int tx, int ty, double f, ColorP c) { - tbox(t, x1, y1, x2, y2, tx, ty, tx + (int) ((x2 - x1) * f), ty + (int) ((y2 - y1) * f), c); -} - -void mogltk::shape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4, int tx, int ty, double f) { - tbox(t, x1, y1, x2, y2, c1, c2, c3, c4, tx, ty, tx + (int) ((x2 - x1) * f), ty + (int) ((y2 - y1) * f)); -} - -void mogltk::shape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, int tx1, int ty1, int tx2, int ty2, ColorP c) { -#if 0 - ENTERT; - - c.Bind(); - t->Bind(); - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2i(tx1, ty1); glVertex2i(x1, y1); - glTexCoord2i(tx2 + 1, ty1); glVertex2i(x2 + 1, y1); - glTexCoord2i(tx1, ty2 + 1); glVertex2i(x1, y2 + 1); - glTexCoord2i(tx2 + 1, ty2 + 1); glVertex2i(x2 + 1, y2 + 1); - glEnd(); - - LEAVE; -#endif -} - -void mogltk::shape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4, int tx1, int ty1, int tx2, int ty2) { -#if 0 - ENTERT; - - t->Bind(); - glBegin(GL_TRIANGLE_STRIP); - c1.Bind(); glTexCoord2i(tx1, ty1); glVertex2i(x1, y1); - c2.Bind(); glTexCoord2i(tx2 + 1, ty1); glVertex2i(x2 + 1, y1); - c3.Bind(); glTexCoord2i(tx1, ty2 + 1); glVertex2i(x1, y2 + 1); - c4.Bind(); glTexCoord2i(tx2 + 1, ty2 + 1); glVertex2i(x2 + 1, y2 + 1); - glEnd(); - - LEAVE; -#endif -} - void mogltk::shape::box3d(int x1, int y1, int x2, int y2, ColorP face, ColorP shade1, ColorP shade2, int depth, bool bevel) { -#if 0 ENTER; - if (!bevel) { - shade1.Bind(); - } else { - shade2.Bind(); + int i; + + for (i = 0; i < depth; i++) { + hline(x1 + i, x2 - i, y1 + i, bevel ? shade2 : shade1); + vline(x1 + i, y1 + i, y2 - i, bevel ? shade2 : shade1); } - glBegin(GL_TRIANGLE_FAN); - glVertex2i(x1, y1); - glVertex2i(x2 + 1, y1); - glVertex2i(x2 + 1 - depth, y1 + depth); - glVertex2i(x1 + depth, y2 + 1 - depth); - glVertex2i(x1, y2 + 1); - glEnd(); - if (!bevel) { - shade2.Bind(); - } else { - shade1.Bind(); + for (i = 0; i < depth; i++) { + hline(x1 + i, x2 - i, y2 - i, bevel ? shade1 : shade2); + vline(x2 - i, y1 + i, y2 - i, bevel ? shade1 : shade2); } - glBegin(GL_TRIANGLE_FAN); - glVertex2i(x2 + 1, y2 + 1); - glVertex2i(x1, y2 + 1); - glVertex2i(x1 + depth, y2 + 1 - depth); - glVertex2i(x2 + 1 - depth, y1 + depth); - glVertex2i(x2 + 1, y1); - glEnd(); - face.Bind(); - glBegin(GL_TRIANGLE_STRIP); - glVertex2i(x1 + depth, y1 + depth); - glVertex2i(x2 + 1 - depth, y1 + depth); - glVertex2i(x1 + depth, y2 + 1 - depth); - glVertex2i(x2 + 1 - depth, y2 + 1 - depth); - glEnd(); + box(x1 + depth, y1 + depth, x2 - depth, y2 - depth, face); LEAVE; -#endif } void mogltk::shape::obox3d(int x1, int y1, int x2, int y2, ColorP shade1, ColorP shade2, bool bevel) { diff --git a/lib/sprite.cc b/lib/sprite.cc index 60f944c..74ea522 100644 --- a/lib/sprite.cc +++ b/lib/sprite.cc @@ -1,5 +1,6 @@ #include -#include "glbase.h" +#include "engine.h" +#include "base.h" #include "sprite.h" #define TEXSIZE 256 @@ -107,6 +108,11 @@ void mogltk::Sprite::TexList::Bind() const { tex->Bind(); } +SDL_Surface * mogltk::Sprite::TexList::GetSurface() { + return tex->GetSurface(); +} + + bool mogltk::Sprite::intersect(int x1, int y1, int x2, int y2) const { int sx1 = posx, sy1 = posy, sx2 = posx + sx, sy2 = posy + sy; @@ -124,27 +130,24 @@ bool mogltk::Sprite::canfit(int x1, int y1, int x2, int y2) const { } void mogltk::Sprite::draw(int dx, int dy, ColorP c) { -#if 0 - bool was2D; + bool locked = false; + + if (SDL_MUSTLOCK(mogltk::engine::base_o->getsurface())) { + locked = true; + SDL_LockSurface(mogltk::engine::base_o->getsurface()); + } - was2D = mogltk::glbase::is2D(); + SDL_Rect src, dst; - if (!was2D) - mogltk::glbase::Enter2DMode(); - - c.Bind(); + src.x = posx; src.y = posy; src.w = sx; src.h = sy; + dst.x = dx; dst.y = dy; dst.w = sx; dst.h = sy; - tlist->Bind(); - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2i(posx , posy ); glVertex2i(dx , dy ); - glTexCoord2i(posx + sx - 1, posy ); glVertex2i(dx + sx - 1, dy ); - glTexCoord2i(posx , posy + sy - 1); glVertex2i(dx , dy + sy - 1); - glTexCoord2i(posx + sx - 1, posy + sy - 1); glVertex2i(dx + sx - 1, dy + sy - 1); - glEnd(); - - if (!was2D) - mogltk::glbase::Leave2DMode(); -#endif + printm(M_INFO, "Drawing sprite at %i %i\n", dx, dy); + + SDL_BlitSurface(tlist->GetSurface(), &src, mogltk::engine::base_o->getsurface(), &dst); + + if (locked) + SDL_UnlockSurface(mogltk::engine::base_o->getsurface()); } void mogltk::Sprite::Bind() { -- cgit v1.2.3