[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Pingus-CVS] [pingus] push by address@hidden - Implemented basic drawing
From: |
pingus |
Subject: |
[Pingus-CVS] [pingus] push by address@hidden - Implemented basic drawing operations for SDL2 on 2014-07-26 21:39 GMT |
Date: |
Sat, 26 Jul 2014 21:40:10 +0000 |
Revision: c5601726f268
Author: Ingo Ruhnke <address@hidden>
Date: Sat Jul 26 21:37:49 2014 UTC
Log: Implemented basic drawing operations for SDL2
http://code.google.com/p/pingus/source/detail?r=c5601726f268
Modified:
/src/engine/display/blitter.cpp
/src/engine/display/opengl/opengl_framebuffer_surface_impl.cpp
/src/engine/display/sdl_framebuffer.cpp
/src/engine/display/sdl_framebuffer_surface_impl.cpp
/src/engine/display/sdl_framebuffer_surface_impl.hpp
/src/engine/display/surface.cpp
/src/engine/input/core_driver.cpp
/src/engine/input/sdl_driver.cpp
=======================================
--- /src/engine/display/blitter.cpp Sat Jul 26 19:58:06 2014 UTC
+++ /src/engine/display/blitter.cpp Sat Jul 26 21:37:49 2014 UTC
@@ -32,7 +32,7 @@
bpp = surface->format->BytesPerPixel;
if (bpp == 1) {
- SDL_Color pal[256];
+ SDL_Palette* pal = SDL_AllocPalette(256);
#ifdef OLD_SDL1
Uint32 ckey;
int useckey = 0;
@@ -48,7 +48,7 @@
new_pixels = static_cast<unsigned char*>(new_surface->pixels);
new_pitch = new_surface->pitch;
- memcpy(pal, surface->format->palette->colors, sizeof(SDL_Color) * 256);
+ memcpy(pal->colors, surface->format->palette->colors,
sizeof(SDL_Color) * 256);
#ifdef OLD_SDL1
ckey = surface->format->colorkey;
#endif
@@ -64,8 +64,8 @@
SDL_UnlockSurface(surface);
SDL_UnlockSurface(new_surface);
+ SDL_SetSurfacePalette(new_surface, pal);
#ifdef OLD_SDL1
- SDL_SetPalette(new_surface, SDL_LOGPAL | SDL_PHYSPAL, pal, 0, 256);
if (useckey) {
SDL_SetColorKey(new_surface, SDL_SRCCOLORKEY | SDL_RLEACCEL, ckey);
}
@@ -75,7 +75,7 @@
float fx, fy, fz;
unsigned char *p1, *p2, *p3, *p4;
- new_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
surface->format->BitsPerPixel,
+ new_surface = SDL_CreateRGBSurface(0, width, height,
surface->format->BitsPerPixel,
surface->format->Rmask,
surface->format->Gmask, surface->format->Bmask, surface->format->Amask);
SDL_LockSurface(surface);
@@ -220,7 +220,7 @@
#ifdef OLD_SDL1
if (surface->flags & SDL_SRCALPHA)
- SDL_SetAlpha(new_surface, SDL_SRCALPHA, surface->format->alpha);
+ SDL_SetSurfaceAlphaMod(new_surface, SDL_SRCALPHA,
surface->format->alpha);
if (surface->format->palette)
SDL_SetPalette(new_surface, SDL_LOGPAL,
surface->format->palette->colors,
=======================================
--- /src/engine/display/opengl/opengl_framebuffer_surface_impl.cpp Sat Jul
26 19:58:06 2014 UTC
+++ /src/engine/display/opengl/opengl_framebuffer_surface_impl.cpp Sat Jul
26 21:37:49 2014 UTC
@@ -42,17 +42,15 @@
// Convert the src surface to a format usable for upload to OpenGL
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
- SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE,
+ SDL_Surface* convert = SDL_CreateRGBSurface(0,
m_texture_size.width,
m_texture_size.height, 32,
0xff000000, 0x00ff0000,
0x0000ff00, 0x000000ff);
#else
- SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE,
+ SDL_Surface* convert = SDL_CreateRGBSurface(0,
m_texture_size.width,
m_texture_size.height, 32,
0x000000ff, 0x0000ff00,
0x00ff0000, 0xff000000);
#endif
-#ifdef OLD_SDL1
- SDL_SetAlpha(src, 0, 0);
-#endif
+ SDL_SetSurfaceAlphaMod(src, 0);
SDL_BlitSurface(src, 0, convert, 0);
GLenum sdl_format;
=======================================
--- /src/engine/display/sdl_framebuffer.cpp Sat Jul 26 19:58:06 2014 UTC
+++ /src/engine/display/sdl_framebuffer.cpp Sat Jul 26 21:37:49 2014 UTC
@@ -153,38 +153,35 @@
FramebufferSurface
SDLFramebuffer::create_surface(const Surface& surface)
{
- return FramebufferSurface(new
SDLFramebufferSurfaceImpl(surface.get_surface()));
+ return FramebufferSurface(new SDLFramebufferSurfaceImpl(m_renderer,
surface.get_surface()));
}
void
SDLFramebuffer::draw_surface(const FramebufferSurface& surface, const
Vector2i& pos)
{
-#ifdef OLD_SDL1
SDLFramebufferSurfaceImpl* impl =
dynamic_cast<SDLFramebufferSurfaceImpl*>(surface.get_impl());
- SDL_Surface* src = impl->get_surface();
+ SDL_Texture* texture = impl->get_texture();
SDL_Rect dstrect;
dstrect.x = static_cast<Sint16>(pos.x);
dstrect.y = static_cast<Sint16>(pos.y);
- dstrect.w = 0;
- dstrect.h = 0;
+ dstrect.w = surface.get_width();
+ dstrect.h = surface.get_height();
- SDL_BlitSurface(src, NULL, screen, &dstrect);
-#endif
+ SDL_RenderCopy(m_renderer, texture, nullptr, &dstrect);
}
void
SDLFramebuffer::draw_surface(const FramebufferSurface& surface, const
Rect& srcrect, const Vector2i& pos)
{
-#ifdef OLD_SDL1
SDLFramebufferSurfaceImpl* impl =
dynamic_cast<SDLFramebufferSurfaceImpl*>(surface.get_impl());
- SDL_Surface* src = impl->get_surface();
+ SDL_Texture* texture = impl->get_texture();
SDL_Rect dstrect;
dstrect.x = static_cast<Sint16>(pos.x);
dstrect.y = static_cast<Sint16>(pos.y);
- dstrect.w = 0;
- dstrect.h = 0;
+ dstrect.w = srcrect.get_width();
+ dstrect.h = srcrect.get_height();
SDL_Rect sdlsrcrect;
sdlsrcrect.x = static_cast<Sint16>(srcrect.left);
@@ -192,144 +189,17 @@
sdlsrcrect.w = static_cast<Uint16>(srcrect.get_width());
sdlsrcrect.h = static_cast<Uint16>(srcrect.get_height());
- SDL_BlitSurface(src, &sdlsrcrect, screen, &dstrect);
-#endif
+ SDL_RenderCopy(m_renderer, texture, &sdlsrcrect, &dstrect);
}
void
SDLFramebuffer::draw_line(const Vector2i& pos1, const Vector2i& pos2,
const Color& color)
{
-#ifdef OLD_SDL1
- int x, y, xlen, ylen, incr;
- int sx = pos1.x;
- int sy = pos1.y;
- int dx = pos2.x;
- int dy = pos2.y;
- draw_pixel_func draw_pixel;
- int clipx1, clipx2, clipy1, clipy2;
- SDL_Rect rect;
-
- SDL_GetClipRect(screen, &rect);
- clipx1 = rect.x;
- clipx2 = rect.x + rect.w - 1;
- clipy1 = rect.y;
- clipy2 = rect.y + rect.h - 1;
-
- // vertical line
- if (sx == dx) {
- if (sx < clipx1 || sx > clipx2 || (sy < clipy1 && dy < clipy1) || (sy
clipy2 && dy > clipy2)) {
- return;
- }
- clip(sy, clipy1, clipy2);
- clip(dy, clipy1, clipy2);
- if (sy < dy) {
- draw_vline(screen, sx, sy, dy - sy + 1, color);
- } else {
- draw_vline(screen, dx, dy, sy - dy + 1, color);
- }
- return;
- }
-
- // horizontal
- if (sy == dy) {
- if (sy < clipy1 || sy > clipy2 || (sx < clipx1 && dx < clipx1) || (sx
clipx2 && dx > clipx2)) {
- return;
- }
- clip(sx, clipx1, clipx2);
- clip(dx, clipx1, clipx2);
- if (sx < dx) {
- draw_hline(screen, sx, sy, dx - sx + 1, color);
- } else {
- draw_hline(screen, dx, dy, sx - dx + 1, color);
- }
- return;
- }
-
- draw_pixel = get_draw_pixel(screen);
- if (!draw_pixel) {
- return;
- }
-
- // exchange coordinates
- if (sy > dy) {
- int t = dx;
- dx = sx;
- sx = t;
- t = dy;
- dy = sy;
- sy = t;
- }
- ylen = dy - sy;
-
- if (sx > dx) {
- xlen = sx - dx;
- incr = -1;
- } else {
- xlen = dx - sx;
- incr = 1;
- }
-
- y = sy;
- x = sx;
-
- if (xlen > ylen) {
- if (sx > dx) {
- int t = sx;
- sx = dx;
- dx = t;
- y = dy;
- }
-
- int p = (ylen << 1) - xlen;
-
- SDL_LockSurface(screen);
- for (x = sx; x < dx; ++x) {
- if (x >= clipx1 && x <= clipx2 && y >= clipy1 && y <= clipy2) {
- draw_pixel(screen, x, y, color);
- }
- if (p >= 0) {
- y += incr;
- p += (ylen - xlen) << 1;
- } else {
- p += (ylen << 1);
- }
- }
- SDL_UnlockSurface(screen);
- return;
- }
-
- if (ylen > xlen) {
- int p = (xlen << 1) - ylen;
-
- SDL_LockSurface(screen);
- for (y = sy; y < dy; ++y) {
- if (x >= clipx1 && x <= clipx2 && y >= clipy1 && y <= clipy2) {
- draw_pixel(screen, x, y, color);
- }
- if (p >= 0) {
- x += incr;
- p += (xlen - ylen) << 1;
- } else {
- p += (xlen << 1);
- }
- }
- SDL_UnlockSurface(screen);
- return;
- }
-
- // Draw a diagonal line
- if (ylen == xlen) {
- SDL_LockSurface(screen);
- while (y != dy) {
- if (x >= clipx1 && x <= clipx2 && y >= clipy1 && y <= clipy2) {
- draw_pixel(screen, x, y, color);
- }
- x += incr;
- ++y;
- }
- SDL_UnlockSurface(screen);
- }
-#endif
+ SDL_SetRenderDrawColor(m_renderer,
+ color.r, color.b, color.g, color.a);
+ SDL_RenderDrawLine(m_renderer,
+ pos1.x, pos1.y,
+ pos2.x, pos2.y);
}
void
@@ -338,91 +208,42 @@
Rect rect = rect_;
rect.normalize();
- draw_line(Vector2i(rect.left, rect.top), Vector2i(rect.right-1,
rect.top), color);
- draw_line(Vector2i(rect.left, rect.bottom-1), Vector2i(rect.right-1,
rect.bottom-1), color);
- draw_line(Vector2i(rect.left, rect.top), Vector2i(rect.left,
rect.bottom-1), color);
- draw_line(Vector2i(rect.right-1, rect.top), Vector2i(rect.right-1,
rect.bottom-1), color);
+ SDL_Rect sdl_rect;
+ sdl_rect.x = rect.left;
+ sdl_rect.y = rect.top;
+ sdl_rect.w = rect.get_width();
+ sdl_rect.h = rect.get_height();
+
+ SDL_SetRenderDrawColor(m_renderer, color.r, color.b, color.g, color.a);
+ SDL_RenderDrawRect(m_renderer, &sdl_rect);
}
void
SDLFramebuffer::fill_rect(const Rect& rect_, const Color& color)
{
-#ifdef OLD_SDL1
Rect rect = rect_;
rect.normalize();
-
- if (color.a == 255)
- {
- SDL_Rect srcrect;
- srcrect.x = static_cast<Sint16>(rect.left);
- srcrect.y = static_cast<Sint16>(rect.top);
- srcrect.w = static_cast<Uint16>(rect.get_width());
- srcrect.h = static_cast<Uint16>(rect.get_height());
+ SDL_Rect sdl_rect;
+ sdl_rect.x = rect.left;
+ sdl_rect.y = rect.top;
+ sdl_rect.w = rect.get_width();
+ sdl_rect.h = rect.get_height();
- SDL_FillRect(m_screen, &srcrect, SDL_MapRGB(screen->format, color.r,
color.g, color.b));
- }
- else if (color.a != 0)
- {
- int top, bottom, left, right;
- int clipx1, clipx2, clipy1, clipy2;
- SDL_Rect cliprect;
-
- SDL_GetClipRect(m_screen, &cliprect);
- clipx1 = cliprect.x;
- clipx2 = cliprect.x + cliprect.w - 1;
- clipy1 = cliprect.y;
- clipy2 = cliprect.y + cliprect.h - 1;
-
- if (rect.right < clipx1 || rect.left > clipx2 || rect.bottom < clipy1 |
| rect.top > clipy2)
- return;
-
- top = rect.top < clipy1 ? clipy1 : rect.top;
- bottom = rect.bottom > clipy2 ? clipy2 : rect.bottom-1;
- left = rect.left < clipx1 ? clipx1 : rect.left;
- right = rect.right > clipx2 ? clipx2 : rect.right-1;
-
- draw_pixel_func draw_pixel = get_draw_pixel(screen);
- if (!draw_pixel)
- return;
-
- SDL_LockSurface(screen);
- for (int y = top; y <= bottom; ++y) {
- for (int x = left; x <= right; ++x) {
- draw_pixel(screen, x, y, color);
- }
- }
- SDL_UnlockSurface(screen);
- }
-#endif
+ SDL_SetRenderDrawColor(m_renderer, color.r, color.b, color.g, color.a);
+ SDL_RenderFillRect(m_renderer, &sdl_rect);
}
void
SDLFramebuffer::flip()
{
-#ifdef OLD_SDL1
- SDL_Flip(screen);
-#endif
+ SDL_RenderPresent(m_renderer);
}
void
SDLFramebuffer::update_rects(const std::vector<Rect>& rects)
{
- std::vector<SDL_Rect> sdl_rects;
-
- for(std::vector<Rect>::const_iterator i = rects.begin(); i !=
rects.end(); ++i)
- {
- SDL_Rect sdl_rect;
- sdl_rect.x = static_cast<Sint16>(i->left);
- sdl_rect.y = static_cast<Sint16>(i->top);
- sdl_rect.w = static_cast<Uint16>(i->get_width());
- sdl_rect.h = static_cast<Uint16>(i->get_height());
- sdl_rects.push_back(sdl_rect);
- }
-
-#ifdef OLD_SDL1
- SDL_UpdateRects(m_screen, static_cast<int>(sdl_rects.size()),
const_cast<SDL_Rect*>(&*sdl_rects.begin()));
-#endif
+ flip();
}
Size
=======================================
--- /src/engine/display/sdl_framebuffer_surface_impl.cpp Sat Jul 26
19:58:06 2014 UTC
+++ /src/engine/display/sdl_framebuffer_surface_impl.cpp Sat Jul 26
21:37:49 2014 UTC
@@ -16,20 +16,16 @@
#include "engine/display/sdl_framebuffer_surface_impl.hpp"
-SDLFramebufferSurfaceImpl::SDLFramebufferSurfaceImpl(SDL_Surface* src) :
- surface()
+SDLFramebufferSurfaceImpl::SDLFramebufferSurfaceImpl(SDL_Renderer*
renderer, SDL_Surface* src) :
+ m_texture(SDL_CreateTextureFromSurface(renderer, src)),
+ m_width(src->w),
+ m_height(src->h)
{
-#ifdef OLD_SDL1
- if (src->format->Amask != 0 || (src->flags & SDL_SRCCOLORKEY))
- surface = SDL_DisplayFormatAlpha(src);
- else
- surface = SDL_DisplayFormat(src);
-#endif
}
SDLFramebufferSurfaceImpl::~SDLFramebufferSurfaceImpl()
{
- SDL_FreeSurface(surface);
+ SDL_DestroyTexture(m_texture);
}
/* EOF */
=======================================
--- /src/engine/display/sdl_framebuffer_surface_impl.hpp Sat Sep 17
21:42:25 2011 UTC
+++ /src/engine/display/sdl_framebuffer_surface_impl.hpp Sat Jul 26
21:37:49 2014 UTC
@@ -22,16 +22,18 @@
class SDLFramebufferSurfaceImpl : public FramebufferSurfaceImpl
{
private:
- SDL_Surface* surface;
-
+ SDL_Texture* m_texture;
+ int m_width;
+ int m_height;
+
public:
- SDLFramebufferSurfaceImpl(SDL_Surface* src);
+ SDLFramebufferSurfaceImpl(SDL_Renderer* renderer, SDL_Surface* src);
~SDLFramebufferSurfaceImpl();
- int get_width() const { return surface->w; }
- int get_height() const { return surface->h; }
+ int get_width() const { return m_width;; }
+ int get_height() const { return m_height; }
- SDL_Surface* get_surface() const { return surface; }
+ SDL_Texture* get_texture() const { return m_texture; }
private:
SDLFramebufferSurfaceImpl(const SDLFramebufferSurfaceImpl&);
=======================================
--- /src/engine/display/surface.cpp Sat Jul 26 19:58:06 2014 UTC
+++ /src/engine/display/surface.cpp Sat Jul 26 21:37:49 2014 UTC
@@ -82,21 +82,17 @@
{
if (colorkey == -1)
{
- impl->surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8,
+ impl->surface = SDL_CreateRGBSurface(0, width, height, 8,
0, 0, 0 ,0);
}
else
{
-#ifdef OLD_SDL1
- impl->surface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCCOLORKEY,
width, height, 8,
+ impl->surface = SDL_CreateRGBSurface(0, width, height, 8,
0, 0, 0 ,0);
- SDL_SetColorKey(impl->surface, SDL_SRCCOLORKEY, colorkey);
-#endif
+ SDL_SetColorKey(impl->surface, SDL_TRUE, colorkey);
}
-#ifdef OLD_SDL1
- SDL_SetColors(impl->surface, palette->colors, 0, palette->ncolors);
-#endif
+ SDL_SetSurfacePalette(impl->surface, palette);
}
Surface::Surface(int width, int height) :
@@ -394,25 +390,17 @@
Surface
Surface::clone() const
{
-#ifdef OLD_SDL1
SDL_Surface* new_surface =
Blitter::create_surface_from_format(impl->surface,
impl->surface->w, impl->surface->h);
- if (impl->surface->flags & SDL_SRCALPHA)
- {
- Uint8 alpha = impl->surface->format->alpha;
- SDL_SetAlpha(impl->surface, 0, 0);
- SDL_BlitSurface(impl->surface, NULL, new_surface, NULL);
- SDL_SetAlpha(impl->surface, SDL_SRCALPHA, alpha);
- }
- else
- {
- SDL_BlitSurface(impl->surface, NULL, new_surface, NULL);
- }
-
+ SDL_BlendMode blend_mode;
+ SDL_GetSurfaceBlendMode(impl->surface, &blend_mode);
+
+ SDL_SetSurfaceBlendMode(impl->surface, SDL_BLENDMODE_NONE);
+ SDL_BlitSurface(impl->surface, NULL, new_surface, NULL);
+
+ SDL_SetSurfaceBlendMode(impl->surface, blend_mode);
+
return Surface(std::shared_ptr<SurfaceImpl>(new
SurfaceImpl(new_surface)));
-#else
- return Surface();
-#endif
}
Surface
@@ -499,9 +487,6 @@
Surface::convert_to_rgba() const
{
SDL_Surface* surface = Blitter::create_surface_rgba(impl->surface->w,
impl->surface->h);
-#ifdef OLD_SDL1
- SDL_SetAlpha(impl->surface, 0, 0);
-#endif
SDL_BlitSurface(impl->surface, NULL, surface, NULL);
return Surface(surface);
}
@@ -510,9 +495,6 @@
Surface::convert_to_rgb() const
{
SDL_Surface* surface = Blitter::create_surface_rgb(impl->surface->w,
impl->surface->h);
-#ifdef OLD_SDL1
- SDL_SetAlpha(impl->surface, 0, 0);
-#endif
SDL_BlitSurface(impl->surface, NULL, surface, NULL);
return Surface(surface);
}
=======================================
--- /src/engine/input/core_driver.cpp Mon Sep 5 20:21:49 2011 UTC
+++ /src/engine/input/core_driver.cpp Sat Jul 26 21:37:49 2014 UTC
@@ -183,23 +183,23 @@
void update(float delta_t)
{
- up->update(delta_t);
- down->update(delta_t);
- left->update(delta_t);
- right->update(delta_t);
+ if (up) up->update(delta_t);
+ if (down) down->update(delta_t);
+ if (left) left->update(delta_t);
+ if (right) right->update(delta_t);
delta.x = delta.y = 0.0f;
- if (left->get_state() == BUTTON_PRESSED)
+ if (left && left->get_state() == BUTTON_PRESSED)
delta.x += speed * delta_t;
- if (right->get_state() == BUTTON_PRESSED)
+ if (right && right->get_state() == BUTTON_PRESSED)
delta.x += -speed * delta_t;
- if (up->get_state() == BUTTON_PRESSED)
+ if (up && up->get_state() == BUTTON_PRESSED)
delta.y += speed * delta_t;
- if (down->get_state() == BUTTON_PRESSED)
+ if (down && down->get_state() == BUTTON_PRESSED)
delta.y += -speed * delta_t;
notify_parent();
=======================================
--- /src/engine/input/sdl_driver.cpp Sat Jul 26 19:58:06 2014 UTC
+++ /src/engine/input/sdl_driver.cpp Sat Jul 26 21:37:49 2014 UTC
@@ -33,16 +33,14 @@
string2key(),
joystick_handles()
{
-#ifdef OLD_SDL1
- for (int i = 0; i < SDLK_LAST; ++i)
+ for (int i = 0; i < SDL_NUM_SCANCODES; ++i)
{
- char* key_name = SDL_GetKeyName(static_cast<SDLKey>(i));
- string2key[key_name] = static_cast<SDLKey>(i);
+ const char* key_name = SDL_GetKeyName(static_cast<SDL_Keycode>(i));
+ string2key[key_name] = static_cast<SDL_Keycode>(i);
// FIXME: Make the keynames somewhere user visible so that users can
use them
log_debug("Key: '" << key_name << "'");
}
-#endif
}
SDLDriver::~SDLDriver()
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Pingus-CVS] [pingus] push by address@hidden - Implemented basic drawing operations for SDL2 on 2014-07-26 21:39 GMT,
pingus <=