반응형
GidPlus란?
- Windows 프로그래밍에서 그래픽을 그리는데 사용되는 라이브러리
- 쉽게 설명하자면, 화면에 그림을 그리기 위한 도구 상자라 생각하면 됨
1. CommonInclude.h 코드 수정
#pragma once
#include <Windows.h>
#include <list>
#include <vector>
#include <algorithm>
#include <minwindef.h>
#include <string>
#include <map>
//추가사항
#include "DirectX_Math.h"
#include <mmsystem.h>
#include <dinput.h>
#pragma comment(lib, "Msimg32.lib")
#pragma comment(lib, "winmm.lib")
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
2. DirectX_Math.h 코드 추가
//DirectX_Math.h
#pragma once
namespace math
{
struct Vector2
{
float x;
float y;
Vector2()
: x(0.0f)
, y(0.0f)
{
}
Vector2(float _x, float _y)
: x(_x)
, y(_y)
{
}
};
}
3. 메인 코드 수정
//헤더 추가
Gdiplus::GdiplusStartupInput gpsi;
//ShutDown추가
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
//윗부분 생략
Gdiplus::GdiplusShutdown(gpToken);
return (int)msg.wParam;
}
//Startup 추가
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
//생략
Gdiplus::GdiplusStartup(&gpToken, &gpsi, NULL);
//씬 로드
dir::LoadScenes();
return TRUE;
}
4.Transform 클래스 수정
//DirectX_Transform.h
#pragma once
#include "DirectX_Component.h"
namespace dir
{
using namespace math;
struct Pos
{
int mX;
int mY;
};
class Transform : public Component
{
public:
Transform();
~Transform();
void Initialize() override;
void Update() override;
void LateUpdate() override;
void Render(HDC hdc) override;
//기존 SetPos에서 vector2를 이용한 걸로 변경
void SetPos(Vector2 pos) { mPosition.x = pos.x; mPosition.y = pos.y; }
Vector2 GetPosition() { return mPosition; }
private:
//변수 또한 vecotor2로 변경
Vector2 mPosition;
};
}
//DirectX_Transform.cpp
#include "DirectX_Transform.h"
namespace dir
{
//생성자 변경
Transform::Transform() : mPosition(Vector2(0, 0))
{
}
Transform::~Transform()
{
}
void Transform::Initialize()
{
}
void Transform::Update()
{
}
void Transform::LateUpdate()
{
}
void Transform::Render(HDC hdc)
{
}
}
5. SpriteRenederer 코드 수정
#pragma once
#include "DirectX_GameObject.h"
#include "DirectX_Component.h"
namespace dir
{
class SpriteRenderer : public Component
{
public:
SpriteRenderer();
~SpriteRenderer();
void Initialize() override;
void Update() override;
void LateUpdate() override;
void Render(HDC hdc) override;
//경로를 받아오는 함수 추가
void ImageLoad(const std::wstring& path);
//Gidplus를 이용해 이미지를 그리도록 수정
private:
Gdiplus::Image* mImgae;
UINT mWidth;
UINT mHeight;
};
}
//DirectX_SpriteRenderer.cpp
#pragma once
#include "DirectX_GameObject.h"
#include "DirectX_Transform.h"
#include "DirectX_SpriteRenderer.h"
namespace dir
{
//생성자 수정
SpriteRenderer::SpriteRenderer() : mImgae(nullptr), mWidth(0), mHeight(0)
{
}
SpriteRenderer::~SpriteRenderer()
{
}
void SpriteRenderer::Initialize()
{
}
void SpriteRenderer::Update()
{
}
void SpriteRenderer::LateUpdate()
{
}
//랜더 방식을 Gidplus를 사용하는 방식으로 변경
void SpriteRenderer::Render(HDC hdc)
{
Transform* tr = GetOwner()->GetComponent<Transform>();
Vector2 pos = tr->GetPosition();
Gdiplus::Graphics graphcis(hdc);
graphcis.DrawImage(mImgae, Gdiplus::Rect(pos.x, pos.y, mWidth, mHeight));
}
//경로를 가져와 이미지를 불러오도록 추가
void SpriteRenderer::ImageLoad(const std::wstring& path)
{
mImgae = Gdiplus::Image::FromFile(path.c_str());
mWidth = mImgae->GetWidth();
mHeight = mImgae->GetHeight();
}
}
6. PlayScene 클래스 변경
//DirectX_PlayScene.cpp
#include "DirectX_PlayScene.h"
#include "DirectX_GameObject.h"
#include "DirectX_Transform.h"
#include "DirectX_SpriteRenderer.h"
#include "DirectX_Player.h"
namespace dir
{
PlayScene::PlayScene()
{
}
PlayScene::~PlayScene()
{
}
void PlayScene::Initialize()
{
{
//오브젝트를 생성한 후
Player* bg = new Player();
Transform* tr = bg->AddComponent<Transform>();
tr->SetPos(Vector2(0, 0));
tr->SetName(L"TR");
//SpriteRenderer 컴포넌트를 추가하여 경로에서 이미지를 불러오도록 추가
SpriteRenderer* sr = bg->AddComponent<SpriteRenderer>();
sr->SetName(L"SR");
sr->ImageLoad(L"D:\\Git\\Win_DirectX_Engine\\Resources\\CloudOcean.png");
AddGameObject(bg);
}
}
void PlayScene::Update()
{
//부모꺼의 함수 호출
Scene::Update();
}
void PlayScene::LateUpdate()
{
Scene::LateUpdate();
}
void PlayScene::Render(HDC hdc)
{
Scene::Render(hdc);
}
}
7. 결과
- 경로에 있는 이미지 파일 그리기 완료
반응형
'C++ > 유니티 엔진 모작(일시중지)' 카테고리의 다른 글
[유니티 엔진 제작] Layer (0) | 2025.02.18 |
---|---|
[유니티 엔진 제작] 씬 변경 및 초기화 (0) | 2025.02.18 |
[유니티 엔진 제작] 컴퍼넌트 구조 (0) | 2025.02.17 |
[유니티 엔진 제작] Scene 제작 (0) | 2025.02.13 |
[유니티 엔진 제작] GameObject 코드 개선 (0) | 2025.02.13 |