SlideShare a Scribd company logo
1 of 8
Download to read offline
#include
SDL_Surface* Background = NULL;
SDL_Surface* SpriteImage = NULL;
SDL_Surface* Backbuffer = NULL;
SDL_Rect SpritePos;
int xPos;
int yPos;
int width=100;
int height=90;
int xDir;
int yDir;
SDL_Surface* LoadImage(char* fileName);
bool LoadFiles();
void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int&
yDirection);
void FreeFiles();
bool ProgramIsRunning();
int main(int argc, char* args[])
{
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
printf("Failed to initialize SDL! ");
return 0;
}
Backbuffer = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
SDL_WM_SetCaption("Color Keying", NULL);
if(!LoadFiles())
{
printf("Failed to load files! ");
FreeFiles();
SDL_Quit();
return 0;
}
SpritePos.x = 0;
SpritePos.y = 0;
while(ProgramIsRunning())
{
xPos = SpritePos.x; //get the current x position
yPos = SpritePos.y; //get the current y position
moveBitMap( xPos, yPos, width, height, xDir, yDir); //move the bitmap
SpritePos.x=xPos; //update x with new posivoid moveBitMap(int& xPosition, int& yPosition, int
width, int height, int& xDirection, int& yDirection)tion
SpritePos.y=yPos; //update y with new position
SDL_BlitSurface(Background, NULL, Backbuffer, NULL);
SDL_BlitSurface(SpriteImage, NULL, Backbuffer, &SpritePos);
SDL_Delay(20);
SDL_Flip(Backbuffer);
}
FreeFiles();
SDL_Quit();
return 0;
}
SDL_Surface* LoadImage(char* fileName)
{
SDL_Surface* imageLoaded = NULL;
SDL_Surface* processedImage = NULL;
imageLoaded = SDL_LoadBMP(fileName);
if(imageLoaded != NULL)
{
processedImage = SDL_DisplayFormat(imageLoaded);
SDL_FreeSurface(imageLoaded);
if(processedImage != NULL)
{
Uint32 colorKey = SDL_MapRGB(processedImage->format, 0, 0, 0);
SDL_SetColorKey(processedImage, SDL_SRCCOLORKEY, colorKey);
}
}
return processedImage;
}
bool LoadFiles()
{
Background = LoadImage("graphics/background.bmp");
if(Background == NULL)
return false;
SpriteImage = LoadImage("graphics/aieplane.bmp");
if(SpriteImage == NULL)
return false;
return true;
}
void FreeFiles()
{
SDL_FreeSurface(Background);
SDL_FreeSurface(SpriteImage);
}
void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int&
yDirection)
{
int x = xPosition;
int y = yPosition;
int dirX = xDirection;
int dirY = yDirection;
x = x + dirX;
//if the bitmap has reached the right side
if(x > 800-width)
{
x = width - 100;
dirX = (-1);
}
if(x= 0)
{
x = 5;
dirX = 1;
}
y = y + dirY;
if(y > 600-height)
{
y = height - 90;//void moveBitMap(int& xPosition, int& yPosition, int width, int height, int&
xDirection, int& yDirection)
dirY = (-1);
}
if(y = 0)
y = 5;
dirY = 1;
xPosition = x;
yPosition = y;
xDirection = dirX;
yDirection = dirY;
}
bool ProgramIsRunning()
{
SDL_Event event;
bool running = true;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
running = false;
}
return running;
}
Solution
#include
SDL_Surface* Background = NULL;
SDL_Surface* SpriteImage = NULL;
SDL_Surface* Backbuffer = NULL;
SDL_Rect SpritePos;
int xPos;
int yPos;
int width=100;
int height=90;
int xDir;
int yDir;
SDL_Surface* LoadImage(char* fileName);
bool LoadFiles();
void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int&
yDirection);
void FreeFiles();
bool ProgramIsRunning();
int main(int argc, char* args[])
{
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
printf("Failed to initialize SDL! ");
return 0;
}
Backbuffer = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
SDL_WM_SetCaption("Color Keying", NULL);
if(!LoadFiles())
{
printf("Failed to load files! ");
FreeFiles();
SDL_Quit();
return 0;
}
SpritePos.x = 0;
SpritePos.y = 0;
while(ProgramIsRunning())
{
xPos = SpritePos.x; //get the current x position
yPos = SpritePos.y; //get the current y position
moveBitMap( xPos, yPos, width, height, xDir, yDir); //move the bitmap
SpritePos.x=xPos; //update x with new posivoid moveBitMap(int& xPosition, int& yPosition, int
width, int height, int& xDirection, int& yDirection)tion
SpritePos.y=yPos; //update y with new position
SDL_BlitSurface(Background, NULL, Backbuffer, NULL);
SDL_BlitSurface(SpriteImage, NULL, Backbuffer, &SpritePos);
SDL_Delay(20);
SDL_Flip(Backbuffer);
}
FreeFiles();
SDL_Quit();
return 0;
}
SDL_Surface* LoadImage(char* fileName)
{
SDL_Surface* imageLoaded = NULL;
SDL_Surface* processedImage = NULL;
imageLoaded = SDL_LoadBMP(fileName);
if(imageLoaded != NULL)
{
processedImage = SDL_DisplayFormat(imageLoaded);
SDL_FreeSurface(imageLoaded);
if(processedImage != NULL)
{
Uint32 colorKey = SDL_MapRGB(processedImage->format, 0, 0, 0);
SDL_SetColorKey(processedImage, SDL_SRCCOLORKEY, colorKey);
}
}
return processedImage;
}
bool LoadFiles()
{
Background = LoadImage("graphics/background.bmp");
if(Background == NULL)
return false;
SpriteImage = LoadImage("graphics/aieplane.bmp");
if(SpriteImage == NULL)
return false;
return true;
}
void FreeFiles()
{
SDL_FreeSurface(Background);
SDL_FreeSurface(SpriteImage);
}
void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int&
yDirection)
{
int x = xPosition;
int y = yPosition;
int dirX = xDirection;
int dirY = yDirection;
x = x + dirX;
//if the bitmap has reached the right side
if(x > 800-width)
{
x = width - 100;
dirX = (-1);
}
if(x= 0)
{
x = 5;
dirX = 1;
}
y = y + dirY;
if(y > 600-height)
{
y = height - 90;//void moveBitMap(int& xPosition, int& yPosition, int width, int height, int&
xDirection, int& yDirection)
dirY = (-1);
}
if(y = 0)
y = 5;
dirY = 1;
xPosition = x;
yPosition = y;
xDirection = dirX;
yDirection = dirY;
}
bool ProgramIsRunning()
{
SDL_Event event;
bool running = true;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
running = false;
}
return running;
}

More Related Content

Similar to #include SDLSDL.hSDL_Surface Background = NULL; SDL_Surface.pdf

How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdfHow do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
footstatus
 

Similar to #include SDLSDL.hSDL_Surface Background = NULL; SDL_Surface.pdf (20)

How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdfHow do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
 
Codes
CodesCodes
Codes
 
Mobile Game and Application with J2ME - Collision Detection
Mobile Gameand Application withJ2ME  - Collision DetectionMobile Gameand Application withJ2ME  - Collision Detection
Mobile Game and Application with J2ME - Collision Detection
 
Mobile Game and Application with J2ME
Mobile Gameand Application with J2MEMobile Gameand Application with J2ME
Mobile Game and Application with J2ME
 
The Ring programming language version 1.9 book - Part 130 of 210
The Ring programming language version 1.9 book - Part 130 of 210The Ring programming language version 1.9 book - Part 130 of 210
The Ring programming language version 1.9 book - Part 130 of 210
 
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + ProcessingRoberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
 
I phone勉強会 (2011.11.23)
I phone勉強会 (2011.11.23)I phone勉強会 (2011.11.23)
I phone勉強会 (2011.11.23)
 
Introduzione a C#
Introduzione a C#Introduzione a C#
Introduzione a C#
 
プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話
 
Double linked list
Double linked listDouble linked list
Double linked list
 
Assignment7.pdf
Assignment7.pdfAssignment7.pdf
Assignment7.pdf
 
Workshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScriptWorkshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScript
 
Scrollytelling
ScrollytellingScrollytelling
Scrollytelling
 
Marat-Slides
Marat-SlidesMarat-Slides
Marat-Slides
 
3
33
3
 
Ssaw08 0624
Ssaw08 0624Ssaw08 0624
Ssaw08 0624
 
Sysprog 11
Sysprog 11Sysprog 11
Sysprog 11
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
 
Snow
SnowSnow
Snow
 
Snow
SnowSnow
Snow
 

More from arasanlethers

AnswerOogenesis is the process by which ovum mother cells or oogo.pdf
AnswerOogenesis is the process by which ovum mother cells or oogo.pdfAnswerOogenesis is the process by which ovum mother cells or oogo.pdf
AnswerOogenesis is the process by which ovum mother cells or oogo.pdf
arasanlethers
 
AnswerB) S. typhimuium gains access to the host by crossing the.pdf
AnswerB) S. typhimuium gains access to the host by crossing the.pdfAnswerB) S. typhimuium gains access to the host by crossing the.pdf
AnswerB) S. typhimuium gains access to the host by crossing the.pdf
arasanlethers
 
A Letter to myself!Hi to myself!Now that I am an Engineer with a.pdf
A Letter to myself!Hi to myself!Now that I am an Engineer with a.pdfA Letter to myself!Hi to myself!Now that I am an Engineer with a.pdf
A Letter to myself!Hi to myself!Now that I am an Engineer with a.pdf
arasanlethers
 
Quantum Numbers and Atomic Orbitals By solving t.pdf
                     Quantum Numbers and Atomic Orbitals  By solving t.pdf                     Quantum Numbers and Atomic Orbitals  By solving t.pdf
Quantum Numbers and Atomic Orbitals By solving t.pdf
arasanlethers
 
A1) A living being or an individual is known as an organism and it i.pdf
A1) A living being or an individual is known as an organism and it i.pdfA1) A living being or an individual is known as an organism and it i.pdf
A1) A living being or an individual is known as an organism and it i.pdf
arasanlethers
 
there are laws and regulations that would pertain to an online breac.pdf
there are laws and regulations that would pertain to an online breac.pdfthere are laws and regulations that would pertain to an online breac.pdf
there are laws and regulations that would pertain to an online breac.pdf
arasanlethers
 
The current article is discussing about the role of SOX4 geneprotei.pdf
The current article is discussing about the role of SOX4 geneprotei.pdfThe current article is discussing about the role of SOX4 geneprotei.pdf
The current article is discussing about the role of SOX4 geneprotei.pdf
arasanlethers
 
Pipelining understandingPipelining is running multiple stages of .pdf
Pipelining understandingPipelining is running multiple stages of .pdfPipelining understandingPipelining is running multiple stages of .pdf
Pipelining understandingPipelining is running multiple stages of .pdf
arasanlethers
 
main.cpp #include iostream #include iomanip #include S.pdf
main.cpp #include iostream #include iomanip #include S.pdfmain.cpp #include iostream #include iomanip #include S.pdf
main.cpp #include iostream #include iomanip #include S.pdf
arasanlethers
 
12). Choose the letter designation that represent homozygous recessi.pdf
12). Choose the letter designation that represent homozygous recessi.pdf12). Choose the letter designation that represent homozygous recessi.pdf
12). Choose the letter designation that represent homozygous recessi.pdf
arasanlethers
 

More from arasanlethers (20)

C, D, and E are wrong and involve random constants. Thisnarrows it d.pdf
C, D, and E are wrong and involve random constants. Thisnarrows it d.pdfC, D, and E are wrong and involve random constants. Thisnarrows it d.pdf
C, D, and E are wrong and involve random constants. Thisnarrows it d.pdf
 
AnswerOogenesis is the process by which ovum mother cells or oogo.pdf
AnswerOogenesis is the process by which ovum mother cells or oogo.pdfAnswerOogenesis is the process by which ovum mother cells or oogo.pdf
AnswerOogenesis is the process by which ovum mother cells or oogo.pdf
 
AnswerB) S. typhimuium gains access to the host by crossing the.pdf
AnswerB) S. typhimuium gains access to the host by crossing the.pdfAnswerB) S. typhimuium gains access to the host by crossing the.pdf
AnswerB) S. typhimuium gains access to the host by crossing the.pdf
 
Answer question1,2,4,5Ion–dipole interactionsAffinity of oxygen .pdf
Answer question1,2,4,5Ion–dipole interactionsAffinity of oxygen .pdfAnswer question1,2,4,5Ion–dipole interactionsAffinity of oxygen .pdf
Answer question1,2,4,5Ion–dipole interactionsAffinity of oxygen .pdf
 
A Letter to myself!Hi to myself!Now that I am an Engineer with a.pdf
A Letter to myself!Hi to myself!Now that I am an Engineer with a.pdfA Letter to myself!Hi to myself!Now that I am an Engineer with a.pdf
A Letter to myself!Hi to myself!Now that I am an Engineer with a.pdf
 
Particulars Amount ($) Millons a) Purchase consideratio.pdf
     Particulars  Amount ($) Millons          a) Purchase consideratio.pdf     Particulars  Amount ($) Millons          a) Purchase consideratio.pdf
Particulars Amount ($) Millons a) Purchase consideratio.pdf
 
2 = 14.191,     df = 9,2df = 1.58 ,         P(2 14.191) = .pdf
2 = 14.191,     df = 9,2df = 1.58 ,         P(2  14.191) = .pdf2 = 14.191,     df = 9,2df = 1.58 ,         P(2  14.191) = .pdf
2 = 14.191,     df = 9,2df = 1.58 ,         P(2 14.191) = .pdf
 
while determining the pH the pH of the water is n.pdf
                     while determining the pH the pH of the water is n.pdf                     while determining the pH the pH of the water is n.pdf
while determining the pH the pH of the water is n.pdf
 
Quantum Numbers and Atomic Orbitals By solving t.pdf
                     Quantum Numbers and Atomic Orbitals  By solving t.pdf                     Quantum Numbers and Atomic Orbitals  By solving t.pdf
Quantum Numbers and Atomic Orbitals By solving t.pdf
 
They are molecules that are mirror images of each.pdf
                     They are molecules that are mirror images of each.pdf                     They are molecules that are mirror images of each.pdf
They are molecules that are mirror images of each.pdf
 
Well u put so many type of compounds here.Generally speakingi) t.pdf
Well u put so many type of compounds here.Generally speakingi) t.pdfWell u put so many type of compounds here.Generally speakingi) t.pdf
Well u put so many type of compounds here.Generally speakingi) t.pdf
 
Ventilation is the process of air going in and out of lungs. Increas.pdf
Ventilation is the process of air going in and out of lungs. Increas.pdfVentilation is the process of air going in and out of lungs. Increas.pdf
Ventilation is the process of air going in and out of lungs. Increas.pdf
 
A1) A living being or an individual is known as an organism and it i.pdf
A1) A living being or an individual is known as an organism and it i.pdfA1) A living being or an individual is known as an organism and it i.pdf
A1) A living being or an individual is known as an organism and it i.pdf
 
there are laws and regulations that would pertain to an online breac.pdf
there are laws and regulations that would pertain to an online breac.pdfthere are laws and regulations that would pertain to an online breac.pdf
there are laws and regulations that would pertain to an online breac.pdf
 
The false statement among the given list is “Territoriality means ho.pdf
The false statement among the given list is “Territoriality means ho.pdfThe false statement among the given list is “Territoriality means ho.pdf
The false statement among the given list is “Territoriality means ho.pdf
 
The current article is discussing about the role of SOX4 geneprotei.pdf
The current article is discussing about the role of SOX4 geneprotei.pdfThe current article is discussing about the role of SOX4 geneprotei.pdf
The current article is discussing about the role of SOX4 geneprotei.pdf
 
The major similarities between rocks and minerals are as follows1.pdf
The major similarities between rocks and minerals are as follows1.pdfThe major similarities between rocks and minerals are as follows1.pdf
The major similarities between rocks and minerals are as follows1.pdf
 
Pipelining understandingPipelining is running multiple stages of .pdf
Pipelining understandingPipelining is running multiple stages of .pdfPipelining understandingPipelining is running multiple stages of .pdf
Pipelining understandingPipelining is running multiple stages of .pdf
 
main.cpp #include iostream #include iomanip #include S.pdf
main.cpp #include iostream #include iomanip #include S.pdfmain.cpp #include iostream #include iomanip #include S.pdf
main.cpp #include iostream #include iomanip #include S.pdf
 
12). Choose the letter designation that represent homozygous recessi.pdf
12). Choose the letter designation that represent homozygous recessi.pdf12). Choose the letter designation that represent homozygous recessi.pdf
12). Choose the letter designation that represent homozygous recessi.pdf
 

Recently uploaded

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 

Recently uploaded (20)

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 

#include SDLSDL.hSDL_Surface Background = NULL; SDL_Surface.pdf

  • 1. #include SDL_Surface* Background = NULL; SDL_Surface* SpriteImage = NULL; SDL_Surface* Backbuffer = NULL; SDL_Rect SpritePos; int xPos; int yPos; int width=100; int height=90; int xDir; int yDir; SDL_Surface* LoadImage(char* fileName); bool LoadFiles(); void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int& yDirection); void FreeFiles(); bool ProgramIsRunning(); int main(int argc, char* args[]) { if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { printf("Failed to initialize SDL! "); return 0; } Backbuffer = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE); SDL_WM_SetCaption("Color Keying", NULL); if(!LoadFiles()) { printf("Failed to load files! "); FreeFiles(); SDL_Quit(); return 0; } SpritePos.x = 0; SpritePos.y = 0;
  • 2. while(ProgramIsRunning()) { xPos = SpritePos.x; //get the current x position yPos = SpritePos.y; //get the current y position moveBitMap( xPos, yPos, width, height, xDir, yDir); //move the bitmap SpritePos.x=xPos; //update x with new posivoid moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int& yDirection)tion SpritePos.y=yPos; //update y with new position SDL_BlitSurface(Background, NULL, Backbuffer, NULL); SDL_BlitSurface(SpriteImage, NULL, Backbuffer, &SpritePos); SDL_Delay(20); SDL_Flip(Backbuffer); } FreeFiles(); SDL_Quit(); return 0; } SDL_Surface* LoadImage(char* fileName) { SDL_Surface* imageLoaded = NULL; SDL_Surface* processedImage = NULL; imageLoaded = SDL_LoadBMP(fileName); if(imageLoaded != NULL) { processedImage = SDL_DisplayFormat(imageLoaded); SDL_FreeSurface(imageLoaded); if(processedImage != NULL) { Uint32 colorKey = SDL_MapRGB(processedImage->format, 0, 0, 0); SDL_SetColorKey(processedImage, SDL_SRCCOLORKEY, colorKey); } } return processedImage; } bool LoadFiles()
  • 3. { Background = LoadImage("graphics/background.bmp"); if(Background == NULL) return false; SpriteImage = LoadImage("graphics/aieplane.bmp"); if(SpriteImage == NULL) return false; return true; } void FreeFiles() { SDL_FreeSurface(Background); SDL_FreeSurface(SpriteImage); } void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int& yDirection) { int x = xPosition; int y = yPosition; int dirX = xDirection; int dirY = yDirection; x = x + dirX; //if the bitmap has reached the right side if(x > 800-width) { x = width - 100; dirX = (-1); } if(x= 0) { x = 5; dirX = 1; } y = y + dirY; if(y > 600-height)
  • 4. { y = height - 90;//void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int& yDirection) dirY = (-1); } if(y = 0) y = 5; dirY = 1; xPosition = x; yPosition = y; xDirection = dirX; yDirection = dirY; } bool ProgramIsRunning() { SDL_Event event; bool running = true; while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) running = false; } return running; } Solution #include SDL_Surface* Background = NULL; SDL_Surface* SpriteImage = NULL; SDL_Surface* Backbuffer = NULL; SDL_Rect SpritePos; int xPos; int yPos; int width=100; int height=90;
  • 5. int xDir; int yDir; SDL_Surface* LoadImage(char* fileName); bool LoadFiles(); void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int& yDirection); void FreeFiles(); bool ProgramIsRunning(); int main(int argc, char* args[]) { if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { printf("Failed to initialize SDL! "); return 0; } Backbuffer = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE); SDL_WM_SetCaption("Color Keying", NULL); if(!LoadFiles()) { printf("Failed to load files! "); FreeFiles(); SDL_Quit(); return 0; } SpritePos.x = 0; SpritePos.y = 0; while(ProgramIsRunning()) { xPos = SpritePos.x; //get the current x position yPos = SpritePos.y; //get the current y position moveBitMap( xPos, yPos, width, height, xDir, yDir); //move the bitmap SpritePos.x=xPos; //update x with new posivoid moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int& yDirection)tion SpritePos.y=yPos; //update y with new position SDL_BlitSurface(Background, NULL, Backbuffer, NULL); SDL_BlitSurface(SpriteImage, NULL, Backbuffer, &SpritePos);
  • 6. SDL_Delay(20); SDL_Flip(Backbuffer); } FreeFiles(); SDL_Quit(); return 0; } SDL_Surface* LoadImage(char* fileName) { SDL_Surface* imageLoaded = NULL; SDL_Surface* processedImage = NULL; imageLoaded = SDL_LoadBMP(fileName); if(imageLoaded != NULL) { processedImage = SDL_DisplayFormat(imageLoaded); SDL_FreeSurface(imageLoaded); if(processedImage != NULL) { Uint32 colorKey = SDL_MapRGB(processedImage->format, 0, 0, 0); SDL_SetColorKey(processedImage, SDL_SRCCOLORKEY, colorKey); } } return processedImage; } bool LoadFiles() { Background = LoadImage("graphics/background.bmp"); if(Background == NULL) return false; SpriteImage = LoadImage("graphics/aieplane.bmp"); if(SpriteImage == NULL) return false; return true; } void FreeFiles()
  • 7. { SDL_FreeSurface(Background); SDL_FreeSurface(SpriteImage); } void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int& yDirection) { int x = xPosition; int y = yPosition; int dirX = xDirection; int dirY = yDirection; x = x + dirX; //if the bitmap has reached the right side if(x > 800-width) { x = width - 100; dirX = (-1); } if(x= 0) { x = 5; dirX = 1; } y = y + dirY; if(y > 600-height) { y = height - 90;//void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int& yDirection) dirY = (-1); } if(y = 0) y = 5; dirY = 1; xPosition = x; yPosition = y;
  • 8. xDirection = dirX; yDirection = dirY; } bool ProgramIsRunning() { SDL_Event event; bool running = true; while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) running = false; } return running; }