mirror of
https://github.com/libSDL2pp/libSDL2pp.git
synced 2025-08-08 13:25:59 -04:00
Axe out remaining uses of old Point/Rect API
This commit is contained in:
parent
3796a6d246
commit
65b5446fd1
@ -28,11 +28,11 @@ try {
|
|||||||
|
|
||||||
renderer.Clear();
|
renderer.Clear();
|
||||||
|
|
||||||
// Also note a way to specify null rects
|
// Also note a way to specify null rects and points
|
||||||
renderer.Copy(sprite1, SDL2pp::Rect::Null(), SDL2pp::Rect::Null());
|
renderer.Copy(sprite1, SDL2pp::NullOpt, SDL2pp::NullOpt);
|
||||||
|
|
||||||
// Copy() is overloaded, providing access to both SDL_RenderCopy and SDL_RenderCopyEx
|
// Copy() is overloaded, providing access to both SDL_RenderCopy and SDL_RenderCopyEx
|
||||||
renderer.Copy(sprite2, SDL2pp::Rect::Null(), SDL2pp::Rect::Null(), 45.0);
|
renderer.Copy(sprite2, SDL2pp::NullOpt, SDL2pp::NullOpt, 45.0);
|
||||||
|
|
||||||
renderer.Present();
|
renderer.Present();
|
||||||
|
|
||||||
|
@ -108,8 +108,6 @@ void Renderer::DrawPoint(int x, int y) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawPoint(const Point& p) {
|
void Renderer::DrawPoint(const Point& p) {
|
||||||
if (p.IsNull())
|
|
||||||
return;
|
|
||||||
DrawPoint(p.GetX(), p.GetY());
|
DrawPoint(p.GetX(), p.GetY());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,8 +115,7 @@ void Renderer::DrawPoints(const Point* points, int count) {
|
|||||||
std::vector<SDL_Point> sdl_points;
|
std::vector<SDL_Point> sdl_points;
|
||||||
sdl_points.reserve(count);
|
sdl_points.reserve(count);
|
||||||
for (const Point* p = points; p != points + count; ++p)
|
for (const Point* p = points; p != points + count; ++p)
|
||||||
if (!p->IsNull())
|
sdl_points.emplace_back(*p);
|
||||||
sdl_points.emplace_back(*p->Get());
|
|
||||||
|
|
||||||
if (SDL_RenderDrawPoints(renderer_, sdl_points.data(), sdl_points.size()) != 0)
|
if (SDL_RenderDrawPoints(renderer_, sdl_points.data(), sdl_points.size()) != 0)
|
||||||
throw Exception("SDL_RenderDrawPoints failed");
|
throw Exception("SDL_RenderDrawPoints failed");
|
||||||
@ -130,17 +127,14 @@ void Renderer::DrawLine(int x1, int y1, int x2, int y2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawLine(const Point& p1, const Point& p2) {
|
void Renderer::DrawLine(const Point& p1, const Point& p2) {
|
||||||
if (p1.IsNull() || p2.IsNull())
|
DrawLine(p1.x, p1.y, p2.x, p2.y);
|
||||||
return;
|
|
||||||
DrawLine(p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawLines(const Point* points, int count) {
|
void Renderer::DrawLines(const Point* points, int count) {
|
||||||
std::vector<SDL_Point> sdl_points;
|
std::vector<SDL_Point> sdl_points;
|
||||||
sdl_points.reserve(count);
|
sdl_points.reserve(count);
|
||||||
for (const Point* p = points; p != points + count; ++p)
|
for (const Point* p = points; p != points + count; ++p)
|
||||||
if (!p->IsNull())
|
sdl_points.emplace_back(*p);
|
||||||
sdl_points.emplace_back(*p->Get());
|
|
||||||
|
|
||||||
if (SDL_RenderDrawLines(renderer_, sdl_points.data(), sdl_points.size()) != 0)
|
if (SDL_RenderDrawLines(renderer_, sdl_points.data(), sdl_points.size()) != 0)
|
||||||
throw Exception("SDL_RenderDrawLines failed");
|
throw Exception("SDL_RenderDrawLines failed");
|
||||||
@ -153,15 +147,11 @@ void Renderer::DrawRect(int x1, int y1, int x2, int y2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawRect(const Point& p1, const Point& p2) {
|
void Renderer::DrawRect(const Point& p1, const Point& p2) {
|
||||||
if (p1.IsNull() || p2.IsNull())
|
DrawRect(p1.x, p1.y, p2.x, p2.y);
|
||||||
return;
|
|
||||||
DrawRect(p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawRect(const Rect& r) {
|
void Renderer::DrawRect(const Rect& r) {
|
||||||
if (r.IsNull())
|
if (SDL_RenderDrawRect(renderer_, &r) != 0)
|
||||||
return;
|
|
||||||
if (SDL_RenderDrawRect(renderer_, r.Get()) != 0)
|
|
||||||
throw Exception("SDL_RenderDrawRect failed");
|
throw Exception("SDL_RenderDrawRect failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,8 +159,7 @@ void Renderer::DrawRects(const Rect* rects, int count) {
|
|||||||
std::vector<SDL_Rect> sdl_rects;
|
std::vector<SDL_Rect> sdl_rects;
|
||||||
sdl_rects.reserve(count);
|
sdl_rects.reserve(count);
|
||||||
for (const Rect* r = rects; r != rects + count; ++r)
|
for (const Rect* r = rects; r != rects + count; ++r)
|
||||||
if (!r->IsNull())
|
sdl_rects.emplace_back(*r);
|
||||||
sdl_rects.emplace_back(*r->Get());
|
|
||||||
|
|
||||||
if (SDL_RenderDrawRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0)
|
if (SDL_RenderDrawRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0)
|
||||||
throw Exception("SDL_RenderDrawRects failed");
|
throw Exception("SDL_RenderDrawRects failed");
|
||||||
@ -183,15 +172,11 @@ void Renderer::FillRect(int x1, int y1, int x2, int y2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::FillRect(const Point& p1, const Point& p2) {
|
void Renderer::FillRect(const Point& p1, const Point& p2) {
|
||||||
if (p1.IsNull() || p2.IsNull())
|
FillRect(p1.x, p1.y, p2.x, p2.y);
|
||||||
return;
|
|
||||||
FillRect(p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::FillRect(const Rect& r) {
|
void Renderer::FillRect(const Rect& r) {
|
||||||
if (r.IsNull())
|
if (SDL_RenderFillRect(renderer_, &r) != 0)
|
||||||
return;
|
|
||||||
if (SDL_RenderFillRect(renderer_, r.Get()) != 0)
|
|
||||||
throw Exception("SDL_RenderFillRect failed");
|
throw Exception("SDL_RenderFillRect failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,20 +184,19 @@ void Renderer::FillRects(const Rect* rects, int count) {
|
|||||||
std::vector<SDL_Rect> sdl_rects;
|
std::vector<SDL_Rect> sdl_rects;
|
||||||
sdl_rects.reserve(count);
|
sdl_rects.reserve(count);
|
||||||
for (const Rect* r = rects; r != rects + count; ++r)
|
for (const Rect* r = rects; r != rects + count; ++r)
|
||||||
if (!r->IsNull())
|
sdl_rects.emplace_back(*r);
|
||||||
sdl_rects.emplace_back(*r->Get());
|
|
||||||
|
|
||||||
if (SDL_RenderFillRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0)
|
if (SDL_RenderFillRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0)
|
||||||
throw Exception("SDL_RenderFillRects failed");
|
throw Exception("SDL_RenderFillRects failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::ReadPixels(const Rect& rect, Uint32 format, void* pixels, int pitch) {
|
void Renderer::ReadPixels(const Optional<Rect>& rect, Uint32 format, void* pixels, int pitch) {
|
||||||
if (SDL_RenderReadPixels(renderer_, rect.Get(), format, pixels, pitch) != 0)
|
if (SDL_RenderReadPixels(renderer_, rect ? &*rect : nullptr, format, pixels, pitch) != 0)
|
||||||
throw Exception("SDL_RenderReadPixels failed");
|
throw Exception("SDL_RenderReadPixels failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::SetClipRect(const Rect& rect) {
|
void Renderer::SetClipRect(const Optional<Rect>& rect) {
|
||||||
if (SDL_RenderSetClipRect(renderer_, rect.Get()) != 0)
|
if (SDL_RenderSetClipRect(renderer_, rect ? &*rect : nullptr) != 0)
|
||||||
throw Exception("SDL_RenderSetClipRect failed");
|
throw Exception("SDL_RenderSetClipRect failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,8 +210,8 @@ void Renderer::SetScale(float scaleX, float scaleY) {
|
|||||||
throw Exception("SDL_RenderSetScale failed");
|
throw Exception("SDL_RenderSetScale failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::SetViewport(const Rect& rect) {
|
void Renderer::SetViewport(const Optional<Rect>& rect) {
|
||||||
if (SDL_RenderSetViewport(renderer_, rect.Get()) != 0)
|
if (SDL_RenderSetViewport(renderer_, rect ? &*rect : nullptr) != 0)
|
||||||
throw Exception("SDL_RenderSetViewport failed");
|
throw Exception("SDL_RenderSetViewport failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,12 +86,12 @@ public:
|
|||||||
void FillRect(const Rect& r);
|
void FillRect(const Rect& r);
|
||||||
void FillRects(const Rect* rects, int count);
|
void FillRects(const Rect* rects, int count);
|
||||||
|
|
||||||
void ReadPixels(const Rect& rect, Uint32 format, void* pixels, int pitch);
|
void ReadPixels(const Optional<Rect>& rect, Uint32 format, void* pixels, int pitch);
|
||||||
|
|
||||||
void SetClipRect(const Rect& rect);
|
void SetClipRect(const Optional<Rect>& rect = NullOpt);
|
||||||
void SetLogicalSize(int w, int h);
|
void SetLogicalSize(int w, int h);
|
||||||
void SetScale(float scaleX, float scaleY);
|
void SetScale(float scaleX, float scaleY);
|
||||||
void SetViewport(const Rect& rect);
|
void SetViewport(const Optional<Rect>& rect = NullOpt);
|
||||||
|
|
||||||
bool TargetSupported();
|
bool TargetSupported();
|
||||||
};
|
};
|
||||||
|
@ -37,5 +37,6 @@
|
|||||||
#include <SDL2pp/ContainerRWops.hh>
|
#include <SDL2pp/ContainerRWops.hh>
|
||||||
#include <SDL2pp/StreamRWops.hh>
|
#include <SDL2pp/StreamRWops.hh>
|
||||||
#include <SDL2pp/Wav.hh>
|
#include <SDL2pp/Wav.hh>
|
||||||
|
#include <SDL2pp/Optional.hh>
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -55,9 +55,9 @@ int Run() {
|
|||||||
|
|
||||||
// Simple copy
|
// Simple copy
|
||||||
float angle = SDL_GetTicks() / 5000.0 * 2.0 * M_PI;
|
float angle = SDL_GetTicks() / 5000.0 * 2.0 * M_PI;
|
||||||
render.Copy(sprite, Rect::Null(), Rect(320 - 64, 240 - 64, 128, 128), angle / M_PI * 180.0);
|
render.Copy(sprite, NullOpt, Rect(320 - 64, 240 - 64, 128, 128), angle / M_PI * 180.0);
|
||||||
render.Copy(sprite, Rect::Null(), Rect(320 - 32 + sin(angle) * 40, 240 - 32 + cos(angle) * 40, 64, 64));
|
render.Copy(sprite, NullOpt, Rect(320 - 32 + sin(angle) * 40, 240 - 32 + cos(angle) * 40, 64, 64));
|
||||||
render.Copy(sprite, Rect::Null(), Rect(320 - 32 - sin(angle) * 40, 240 - 32 - cos(angle) * 40, 64, 64));
|
render.Copy(sprite, NullOpt, Rect(320 - 32 - sin(angle) * 40, 240 - 32 - cos(angle) * 40, 64, 64));
|
||||||
|
|
||||||
render.Present();
|
render.Present();
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ int Run() {
|
|||||||
// Sprite data
|
// Sprite data
|
||||||
Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, MY_SPRITE_SIZE, MY_SPRITE_SIZE);
|
Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, MY_SPRITE_SIZE, MY_SPRITE_SIZE);
|
||||||
|
|
||||||
sprite.Update(Rect::Null(), pixels, MY_SPRITE_SIZE * MY_SPRITE_SIZE);
|
sprite.Update(NullOpt, pixels, MY_SPRITE_SIZE * MY_SPRITE_SIZE);
|
||||||
sprite.SetBlendMode(SDL_BLENDMODE_BLEND);
|
sprite.SetBlendMode(SDL_BLENDMODE_BLEND);
|
||||||
|
|
||||||
// Two render target textures
|
// Two render target textures
|
||||||
@ -93,10 +93,10 @@ int Run() {
|
|||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
render.SetTarget(target2);
|
render.SetTarget(target2);
|
||||||
render.Clear();
|
render.Clear();
|
||||||
render.Copy(target1, Rect::Null(), Rect(0, 0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
|
render.Copy(target1, NullOpt, Rect(0, 0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
|
||||||
render.Copy(target1, Rect::Null(), Rect(MY_RENDERTARGET_SIZE / 2, 0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
|
render.Copy(target1, NullOpt, Rect(MY_RENDERTARGET_SIZE / 2, 0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
|
||||||
render.Copy(target1, Rect::Null(), Rect(0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
|
render.Copy(target1, NullOpt, Rect(0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
|
||||||
render.Copy(target1, Rect::Null(), Rect(MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
|
render.Copy(target1, NullOpt, Rect(MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
|
||||||
|
|
||||||
// Swap textures to copy recursively
|
// Swap textures to copy recursively
|
||||||
std::swap(target1, target2);
|
std::swap(target1, target2);
|
||||||
@ -106,7 +106,7 @@ int Run() {
|
|||||||
render.SetTarget();
|
render.SetTarget();
|
||||||
render.Clear();
|
render.Clear();
|
||||||
|
|
||||||
render.Copy(target1, Rect::Null(), Rect((MY_SCREEN_WIDTH - MY_SCREEN_HEIGHT) / 2, 0, MY_SCREEN_HEIGHT, MY_SCREEN_HEIGHT), SDL_GetTicks() / 10000.0 * 360.0);
|
render.Copy(target1, NullOpt, Rect((MY_SCREEN_WIDTH - MY_SCREEN_HEIGHT) / 2, 0, MY_SCREEN_HEIGHT, MY_SCREEN_HEIGHT), SDL_GetTicks() / 10000.0 * 360.0);
|
||||||
|
|
||||||
render.Present();
|
render.Present();
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ int Run() {
|
|||||||
|
|
||||||
// Load sprite texture
|
// Load sprite texture
|
||||||
Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 4, 4);
|
Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 4, 4);
|
||||||
sprite.Update(Rect::Null(), pixels, 4 * 4);
|
sprite.Update(NullOpt, pixels, 4 * 4);
|
||||||
sprite.SetBlendMode(SDL_BLENDMODE_BLEND);
|
sprite.SetBlendMode(SDL_BLENDMODE_BLEND);
|
||||||
|
|
||||||
render.SetDrawBlendMode(SDL_BLENDMODE_BLEND);
|
render.SetDrawBlendMode(SDL_BLENDMODE_BLEND);
|
||||||
@ -63,23 +63,23 @@ int Run() {
|
|||||||
render.Clear();
|
render.Clear();
|
||||||
|
|
||||||
// Simple copy
|
// Simple copy
|
||||||
render.Copy(sprite, Rect::Null(), Rect(80, 0, 240, 240));
|
render.Copy(sprite, NullOpt, Rect(80, 0, 240, 240));
|
||||||
|
|
||||||
// Copy with modulation
|
// Copy with modulation
|
||||||
render.Copy(sprite, Rect::Null(), Rect(400, 0, 120, 120));
|
render.Copy(sprite, NullOpt, Rect(400, 0, 120, 120));
|
||||||
sprite.SetAlphaMod(92);
|
sprite.SetAlphaMod(92);
|
||||||
render.Copy(sprite, Rect::Null(), Rect(400 + 120, 0, 120, 120));
|
render.Copy(sprite, NullOpt, Rect(400 + 120, 0, 120, 120));
|
||||||
sprite.SetColorMod(255, 0, 0);
|
sprite.SetColorMod(255, 0, 0);
|
||||||
render.Copy(sprite, Rect::Null(), Rect(400, 0 + 120, 120, 120));
|
render.Copy(sprite, NullOpt, Rect(400, 0 + 120, 120, 120));
|
||||||
sprite.SetAlphaMod();
|
sprite.SetAlphaMod();
|
||||||
render.Copy(sprite, Rect::Null(), Rect(400 + 120, 0 + 120, 120, 120));
|
render.Copy(sprite, NullOpt, Rect(400 + 120, 0 + 120, 120, 120));
|
||||||
sprite.SetColorMod();
|
sprite.SetColorMod();
|
||||||
|
|
||||||
// Copy with rotation
|
// Copy with rotation
|
||||||
render.Copy(sprite, Rect::Null(), Rect(80, 240, 240, 240), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, Point::Null(), SDL_FLIP_NONE);
|
render.Copy(sprite, NullOpt, Rect(80, 240, 240, 240), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, NullOpt, SDL_FLIP_NONE);
|
||||||
|
|
||||||
// Rotation around another point
|
// Rotation around another point
|
||||||
render.Copy(sprite, Rect::Null(), Rect(520, 360, 120, 120), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, Point(0, 0), SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL);
|
render.Copy(sprite, NullOpt, Rect(520, 360, 120, 120), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, Point(0, 0), SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL);
|
||||||
|
|
||||||
render.Present();
|
render.Present();
|
||||||
|
|
||||||
|
@ -78,8 +78,6 @@ BEGIN_TEST()
|
|||||||
EXPECT_TRUE(r != Rect(1,3,3,4));
|
EXPECT_TRUE(r != Rect(1,3,3,4));
|
||||||
EXPECT_TRUE(r != Rect(1,2,4,4));
|
EXPECT_TRUE(r != Rect(1,2,4,4));
|
||||||
EXPECT_TRUE(r != Rect(1,2,3,5));
|
EXPECT_TRUE(r != Rect(1,2,3,5));
|
||||||
EXPECT_TRUE(r.Get() != nullptr);
|
|
||||||
EXPECT_TRUE(r.Get()->x == 1 && r.Get()->y == 2 && r.Get()->w == 3 && r.Get()->h == 4);
|
|
||||||
EXPECT_TRUE(r.x == 1 && r.y == 2 && r.w == 3 && r.h == 4);
|
EXPECT_TRUE(r.x == 1 && r.y == 2 && r.w == 3 && r.h == 4);
|
||||||
|
|
||||||
r.SetX(5);
|
r.SetX(5);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user