Cub3d 학습일지 - 5 - C로 시작하기

대표 사진

(유료 강의입니다. 12.99$)

 

Raycasting Development with C

Create a raycasting 3D scene using C programming language

courses.pikuma.com

제 5강 Starting out C Project 이름

5.1  First Steps in C C에서의 첫걸음
5.T 
A Heads Up For Windows Developers 윈도우 개발자를 위한 Heads up
5.2 
Working with Makefile Makefile 만들면서 작업하기
5.3  Installing VS and SDL on Windows VS와 SDL 설치하기 (윈도우)
5.4
 Creating a SDL Window SDL 윈도우 만들기
5.5
 SDL Rendering and Event Polling SDL 렌더링 and 이벤트 폴링
5.6
Rendering SDL Rectangles SDL 사각형 렌더링하기

5.1  First Steps in C C에서의 첫걸음

컴파일 방식: gcc *.c -o raycast

작업환경(me) : 맥북 pro

SDL 설치 : brew install sdl2

 

결과

Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> New Formulae
cargo-audit  dprint       libxml++@4   md4c         openalpr     ruby@2.7
cargo-watch  h2spec       libxml++@5   mermaid-cli  pkger
dnsx         keptn        massdns      mockery      rttr
==> Updated Formulae
Updated 2576 formulae.
==> Deleted Formulae
gobby                                    godep
==> New Casks
bluewallet                               kekaexternalhelper
circuitjs1                               okta-advanced-server-access
dyalog                                   operator
epoccam                                  silicon
futubull                                 smultron
fvim                                     tencent-docs
==> Updated Casks
Updated 373 casks.
==> Deleted Casks
eventstore          futuniuniu          kekadefaultapp      scaleft

==> Downloading https://homebrew.bintray.com/bottles/sdl2-2.0.14_1.big_sur.bottl
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/ccde7145d4334d9274f95
######################################################################## 100.0%
==> Pouring sdl2-2.0.14_1.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/sdl2/2.0.14_1: 91 files, 5.1MB

5.T  A Heads Up For Windows Developers 윈도우 개발자를 위한 Heads up
더보기

A Quick Warning for Windows Developers

If you are going to follow the course on Windows, I will explain very soon how to configure Visual Studio to work with SDL.

But I have a suggestion for Windows users. Microsoft is not very good with keeping up with the newer versions of the C language. Therefore, before I go ahead and teach you how to configure Visual Studio on Windows, please watch the next lecture where I configure the project using only the Unix command-line. I truly believe understanding the basics of the Unix command-line is very important for every developer and building our source code using only the terminal helps students understand what is really happening with the code being compiled.

So, let's learn how to compile using the Unix terminal with the gcc compiler and use a simple Makefile to build our project. Once we are done, then I'll come back and teach you how to configure SDL to work with Visual Studio.


5.2  Working with Makefile Makefile 만들면서 작업하기

먼저 디렉토리 구조를 만드세요.

raycasting-c (폴더명은 상관없음)
> Makefile
> src
> > main.c
> > constant.h
// Makefile

build:
	gcc -std=c99 ./src/*.c -lSDL2 -o raycast

run:
	./raycast

clean:
	rm raycast;
// main.c

#include "constants.h"
#include <stdio.h>

int			main()
{
	// Pikuma 선생님은 puts 함수를 사용하셨습니다.
	printf("Program is running.\n");
	
	return (0);
}
// constant.h

#ifndef CONSTANTS_H
#define CONSTANTS_H

# include <unistd.h>

#endif

5.3  Installing VS and SDL on Windows VS와 SDL 설치하기 (윈도우)

skip


5.4  Creating a SDL Window SDL 윈도우 만들기
5.5 
SDL Rendering and Event Polling SDL 렌더링 and 이벤트 폴링
5.6 
Rendering SDL Rectangles SDL 사각형 렌더링하기

github.com/KKWANH/cub3d_kkim/tree/main/C0_Basic_RunningSDL

 

KKWANH/cub3d_kkim

Contribute to KKWANH/cub3d_kkim development by creating an account on GitHub.

github.com

// main.c

#include			"constants.h"
#include			<stdio.h>

SDL_Window			*window				= NULL;
SDL_Renderer		*renderer			= NULL;
int					isGameRunning		= FALSE;
int					playerX				= 0;
int					playerY				= 0;

int					initializeWindow(void)
{
	if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
	{ fprintf(stderr, "Error: initializing SDL\n");			return	(FALSE);}

	window = SDL_CreateWindow(
		NULL,
		SDL_WINDOWPOS_CENTERED,		SDL_WINDOWPOS_CENTERED,
		WINDOW_WIDTH, 				WINDOW_HEIGHT,
		SDL_WINDOW_BORDERLESS
	);
	if (!window)
	{ fprintf(stderr, "Error: creating SDL window\n");		return	(FALSE); }

	renderer = SDL_CreateRenderer(window, -1, 0);
	if (!renderer)
	{ fprintf(stderr, "Error: creating SDL renderer\n");		return	(FALSE); }

	SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
	
	return	(TRUE);
}

void				setup(void)
{
	// TODO: initialize game objects and variables
}

void				processInput(void)
{
	SDL_Event		event;
	SDL_PollEvent(&event);
	switch (event.type)
	{
		case SDL_QUIT:
		{ isGameRunning = FALSE; break; }
		case SDL_KEYDOWN:
		{
			if (event.key.keysym.sym == SDLK_ESCAPE)
				isGameRunning = FALSE;
			break;
		}
	}
}

void				update(void)
{
	++playerX;
	++playerY;
}

void				render(void)
{
	SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
	SDL_RenderClear(renderer);

	// TODO: render all game objects for the current frame
	SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
	SDL_Rect		rect = {playerX, playerY, 20, 20};
	SDL_RenderFillRect(renderer, &rect);

	SDL_RenderPresent(renderer);
}

void				destroyWindow(void)
{
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	SDL_Quit();
}

int					main(void)
{
	isGameRunning = initializeWindow();
	setup();
	while (isGameRunning)
	{
		processInput();
		update();
		render();
	}
	destroyWindow();
	return (0);
}
#ifndef CONSTANTS_H
#define CONSTANTS_H

# include	<unistd.h>
# include	<SDL2/SDL.h>

# define	FALSE				0
# define	TRUE				1
# define	WINDOW_WIDTH		800
# define	WINDOW_HEIGHT		600

#endif